1

this issue is driving me a little crazy so hopefully one of you fine people could point me in the right direction. I am attempting to prepare a JSONObject that will be passed from client to server. The following is the problematic method stripped down to its essentials:

private JSONObject getJsonParam(int id)
{
    JSONObject param = new JSONObject();
    try
    {
        param.put("functionCode", 50);
        param.put("id", id);
        return param;
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }
    catch (Throwable e)
    {
        e.printStackTrace();
    }
    return null;
}

I have carefully traced the code in debug mode. In the actual method, I put many more paramters in the JSONObject and all is well until the last param.put() method where I attempt to insert the id. When this is the current execution line, I can visualize the param variable and all looks good. Then when I perform the step function to execute the last param.put call, it jumps to the return null statement. I have put breakpoints in both catch blocks and neither are being executed it seems. (I added the second catch block to make sure no other throwable was causing issues).

Any ideas what would be causing this odd behavior? I even tried rearranging the order of the put calls. It does not have any issue with a particular put call, but just the last one before the return statement.

I am working in Android/Java.

3
  • what error you are getting, pl post your stacktrace Commented Oct 6, 2014 at 16:02
  • 1
    That might be an issue with the IDE. Make sure your result is the JSONObject and not null by putting a breakpoint outside the function and checking what it returned. Commented Oct 6, 2014 at 16:06
  • @PedroOliveira It was indeed an issue with IDE. when I did as you suggested, I found that the function was returning the proper JSONObject! Thank you! I suppose this means I wasted a lot of time trying to solve a problem that was not actually a problem... haha Commented Oct 6, 2014 at 16:16

1 Answer 1

1

The code is not causing an error - you are merely being caught out by the fact that the compiler has optimized both return statements to use the same code.

In each case the return is of some "current value", in one case the current value comes from the param variable, in the other case it is null. Since the compiler uses just one copy of the code, the debugger is unable to determine correctly which actual line of source is being executed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.