• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

No value for jsonObject getString()

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​
 
Last edited:
  • Like
Reactions: das23489679
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?

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!
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones