1. Download our Official Android App: Forums for Android!

No value for jsonObject getString()

Discussion in 'Android Development' started by exceedbeyond, Sep 11, 2017.

  1. exceedbeyond

    exceedbeyond Lurker
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    5
    Joined:
    Sep 11, 2017

    Sep 11, 2017
    5
    1
    16
    I have a json data that looks like the following:

    {"bills":[{"BillID":"379","BillName":"Credit Card","Amount":"$700.00","PayType":"Auto","Status":"Not Due","DateDue":"2017-03-15","Title":"DUE!","BillSchedule":"90","BillNote":"Test","BillCategory":"Home Expense\/Utilities\/Gas"}]}
    When I do the code below in Android Studio:
    JSONObject jsonObject = new JSONObject(result);
    jsonArray = jsonObject.getJSONArray("bills");

    for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jsonOBject = jsonArray.getJSONObject(i);

    String billID = jsonOBject.getString("BillID");
    String billName = jsonOBject.getString("BillName");
    String billAmount = jsonOBject.getString("Amount");
    String payType = jsonOBject.getString("PayType");
    tring billStatus = jsonOBject.getString("Status");
    String billDueDate = jsonOBject.getString("DateDue");
    String title = jsonOBject.getString("Title");
    String billSchedule = jsonObject.getString("BillSchedule");
    String billNote = jsonOBject.optString("BillNote");
    String category = jsonObject.optString("BillCategory");

    BillObject data = new BillObject(billID,billName,billAmount, payType, billStatus,billDueDate, title,billSchedule,billNote,billCategory);

    data_list.add(data);

    }​

    It raises an Json exception stating there is "no value for BillCategory". As you can see by the Json data the BillSchedule field is being populated. When I debug the app and put a break point near the variable, the Json data object value is clearly there as well. I have tried assigning a value that doesn't have slashes as a test, with no success. Can someone please help me figure this out, I don't know what I am missing.

    I should clarify when I use jsonObject.getString I get "No Value for BillCategory" , when I use jsonObject.optString it returns null even though BillCategory has a value on Json data object​


     

    Advertisement

    #1 exceedbeyond, Sep 11, 2017
    Last edited: Sep 11, 2017
    das23489679 likes this.
  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,450
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,450
    10,736
    1,988
    Male
    Software developer
    South West of England
    Very odd. Have you tried breakpointing at the line where it's trying to retrieve the BillCategory value, and stepping into the optString() method to see what it's doing?
    What's the actual exception you get? Can you post the stack trace?
     
  3. exceedbeyond

    exceedbeyond Lurker
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    5
    Joined:
    Sep 11, 2017

    Sep 11, 2017
    5
    1
    16
    I have not tried stepping into the method. I figured since it was working on the other values that is had to be something up with my code, but I will try that. The specific error is "No value for BillCategory". Thanks for your time!
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,450
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,450
    10,736
    1,988
    Male
    Software developer
    South West of England
    Never mind, I see your problem

    Code (Text):
    1.  
    2. String category = jsonObject.optString("BillCategory");
    3.  
    Should be

    Code (Text):
    1.  
    2. String category = jsonOBject.optString("BillCategory");
    3.  
     
  5. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,450
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,450
    10,736
    1,988
    Male
    Software developer
    South West of England
    Spot the difference :)
     
  6. exceedbeyond

    exceedbeyond Lurker
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    5
    Joined:
    Sep 11, 2017

    Sep 11, 2017
    5
    1
    16
    WOWWWWW!!!!!!!! That is crazy!! oh man..I can't believe that! Thank you so much!
     
  7. exceedbeyond

    exceedbeyond Lurker
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    5
    Joined:
    Sep 11, 2017

    Sep 11, 2017
    5
    1
    16
    "json.OBject vs jsonObject"
     
  8. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,450
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,450
    10,736
    1,988
    Male
    Software developer
    South West of England
    Well the thing to learn from that is you have to be careful about naming your variables. A subtle difference like that is not such a good idea.
     
  9. exceedbeyond

    exceedbeyond Lurker
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    5
    Joined:
    Sep 11, 2017

    Sep 11, 2017
    5
    1
    16
    I will not say how long I have overlooked that :rolleyes:, but the help was much appreciated and lesson learned. Thx again!
     

Share This Page

Loading...