• 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:
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?
 
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!
 
Never mind, I see your problem

Code:
String category = jsonObject.optString("BillCategory");

Should be

Code:
String category = jsonOBject.optString("BillCategory");
 
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.
 
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.
I will not say how long I have overlooked that :rolleyes:, but the help was much appreciated and lesson learned. Thx again!
 
Back
Top Bottom