I’m developing an application for which i’m decoding the JSON response using java. Following is the code snippet where i’m getting occasional NullPointerException
JSONParser parser=new JSONParser();
URL url=new URL("http://api.yummly.com/v1/api/recipes?q="+
URLEncoder.encode(dish,"UTF- 8")+"&_app_id=APP_ID8&_app_key=APP_KEY");
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
Object json=parser.parse(br);
JSONObject obj=(JSONObject)json;
if(obj!=null)
count=(long)obj.get("totalMatchCount"); <--- Exception at this point
this statement is running in a loop and giving NullPointerException at random iterations.
It’s telling you that obj.get(“totalMatchCount”) is returning null, and blowing up in the cast to a long.
Something like
Now why, whether and if it should be null, is something else.