1

Hitting a brick wall in my code at the moment for fetching json objects from multiple pages(using a loop) in a AsyncTask. It reaches the last page, but getting the correct if statement to ensure that the loop DOESN'T run again and continues on is baffling me.

    String data = //some correct json data with next element that holds a uri parseable string
    JSONObject initial = new JSONObject(data);
    String next = initial.getString(nextObjSTR);
    //gonna start from the "last" page and recursively return to the 1st page
    if(*The if condition I need help with*) {
        //there is another page
        makeConnection(Uri.parse(next));
    }

Basically, the last page of json elements has a next element with a null or no element value, which triggers the IOException error caught in makeConnection method because my initial if statement has always been failing.

Can I get a reason or help as to the appropriate if check for Strings from json? I've tried String != null as NullPointerExceptions occur if I use any method from String to compare. Likewise, JsonObject.NULL comparison doesn't work for me either.

4
  • Make sure your data is not null Commented Sep 11, 2016 at 10:04
  • Put the code in a try block and catch the exception. Print exception details using logging and post them back here. That'll help us more. Commented Sep 11, 2016 at 11:02
  • Well, The data has to be null at some point, It works for the first 12 pages of data. Also, there is a try catch block in makeconnection that catches the error, so that is also irrelevant. Commented Sep 11, 2016 at 18:07
  • getString throws an exception when the key does not exist in the JSON. If the null-check isn't working, then you don't have a null value. You might have an empty string. Commented Sep 11, 2016 at 20:26

5 Answers 5

3

None of the other answers worked, and I ended up questioning whether the element was really null despite looking at the parsed json data via an online tool. In the end, JSONObject.IsNull(element mapping name) is the right approach.

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

Comments

2

you must check value with key is has in json object. Try below code:

JSONObject initial = new JSONObject(data);
if(initial.has(nextObjSTR)) {
    String next = initial.getString(nextObjSTR);
    if (next != null && !next.isEmpty()) {
        makeConnection(Uri.parse(next));
    }               
}

Comments

1

If you're sure that the value is either null (empty) or a correct URI, and assuming that the nextObjSTR key is always present in the data JSON, then that will do:

if (next != null && !next.trim().isEmpty()) {
   makeConnection(Uri.parse(next));
}

Or, since you're on Android, it's better use the more convenient method:

if (!TextUtils.isEmpty(next)) {
   makeConnection(Uri.parse(next));
}

Comments

1

You can use the optString Method of the JSONObject. If the JSON key is not this method will return a empty string, so you can check it easily:

String next = initial.optString(nextObjSTR);
if ( ! next.isEmpty() ) {
   makeConnection(Uri.parse(next));
}

Source: https://developer.android.com/reference/org/json/JSONObject.html#optString(java.lang.String)

1 Comment

Sorry, I'm rather new in Java and not compared (or check if zero) yet. I've correct it
0

I do like this...

String value; 
if(jsonObject.get("name").toString.equals("null")){
  value = "";
else{
   value = jsonObject.getString("name");
}

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.