Anonymous Hacker Anonymous Hacker - 24 days ago 7
JSON Question

Making mistake in JSON

I am facing problem


java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference

I am sure i am making mistake in fetching data from the J-SON Response

here what i am doing

JSONObject json = new JSONObject(incomingRecover_JSON);
JSONArray dive = json.getJSONArray("items");

for (int i=0 ; i< dive.length() ; i++) {

JSONObject dive_deep = dive.getJSONObject(i);

JSONObject dive_more_deep = dive_deep.getJSONObject("volumeInfo");

String title = dive_more_deep.getString("title");
String sub_title = dive_more_deep.getString("subtitle");

Data_Holder store_things = new Data_Holder(title, sub_title);


**and here is the J-SON **

Answer Source

Not all volumeInfo has title and subtitle.

getString() method returns null when there is no key.

Use try catch to handle null.