3

I have the following method for parsing a sample String to JSONObject:

private JSONObject test() {
    try {

        String responseData = "{\"m_tani\":[{\"tani_cd\":\"02\",\"tani_nm\":\"cs\"},{\"tani_cd\":\"03\",\"tani_nm\":\"pc\"}]}";
        Log.i("Json", responseData.toString());
        JSONObject json = new JSONObject(responseData);

        return json;

    } catch (Exception e) {
        e.printStackTrace();
        Log.i("Json", "exception");
    }
    Log.i("Json", null);
    return null;
}

The responseData is:

{"m_tani":[{"tani_cd":"02","tani_nm":"cs"},{"tani_cd":"03","tani_nm":"pc"}]}

When I debug it, from the line JSONObject json = new JSONObject(responseData); it jumps to return null;, not return json; or catch(Exception e).

I don't know why, please help me with this

7
  • Did you try to actually print out the result, to see what the function returns? Eclipse debug-steps may behave this way on multiple return statements, but if your code is correct, the method's output will still be correct! @Simulant, there is no exception, this has been made clear in the question. Commented May 8, 2014 at 7:50
  • I have tried your code, and is found working fine. Please post more details if you have. Commented May 8, 2014 at 7:54
  • Is there a chance where you debus your old code in the devices and line numbers wont match. Jumps are caused by this usually. Commented May 8, 2014 at 7:56
  • I uninstall this app and install again, It work fine. But, I still don't know why. Commented May 8, 2014 at 8:01
  • Declare JSONObject in first line and change last line return json; Commented May 8, 2014 at 8:14

2 Answers 2

0

This occurs during step-by-step debugging, when you have multiple return points from a method.

When converting the java bytecode to dalvik, the return calls are merged (for optimization reasons?), and when you debug the code, it may seem that you reach for the wrong one, or call multiple ones. That's not happening though, your code is correct, it's just how it appears when debugging.

You can see this post for more reference

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

2 Comments

You're right. Although it jump to "return null;" statement when I'm debugging. The result is not null.
I'm glad if it helped. It's a very annoying thing, but you can get used to it. Eventually steps in the dalvik bytecode may be interpreted in their real meanings, and this issue disappears.
-2

try this code

    private JSONObject test() {
    try {
        String responseData = "{\"m_tani\":[{\"tani_cd\":\"02\",\"tani_nm\":\"cs\"},{\"tani_cd\":\"03\",\"tani_nm\":\"pc\"}]}";
        Log.i("Json", responseData.toString());
        JSONObject json = new JSONObject(responseData);
        return json;
    } catch (JSONException e) {
        e.printStackTrace();
        Log.i("Json", "exception");
        return null;
    }
}

1 Comment

please atleast give reason for voting down

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.