Skip to content Skip to sidebar Skip to footer

Line -> JSONArray JArray=new JSONArray(result); Giving Nullpointer Exception

package com.example.fyptrialapp; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap;

Solution 1:

JSON syntax never starts as an array. It could be that it starts with { followed by [, but never straight to [. This is why trying to convert your results to JSONArray will not work. You should therefor try to get the object first, then the array elements.

JSONObject jo = new JSONObject(result);
JSONArray jArray = jo.getJSONArray("keyIdentifyer");

If you dont have the array key identifyer, you could try to iterate or get the first element. See this post answear on iterations when no keys are present.


Solution 2:

You should indeed use AsyncTask for the networking part. Your app probably already crashes with a NetworkOnMainThread error follow this link for the usasge of AsyncTask.

But that besides, you are providing to little code to point to a reason for the NullPointerException. You should always check if an object is not null if it may occur it is.

try{
   //your code
}catch(NullPointerException ex){
   ex.printStackTrace();
}

Post a Comment for "Line -> JSONArray JArray=new JSONArray(result); Giving Nullpointer Exception"