Async Android Json Parser Android- Null Pointer Exception
Solution 1:
It looks like you're using this JSONParser I think. What appears to be happening is that you have a URL that is not producing valid JSON, which is resulting in an Exception
being thrown and caught in the class somewhere along the way -- most likely in the jObj = new JSONObject(json);
line. The end result is that the returned variable is still null
. So when you call json.length()
in your loop, you're trying to call length()
on a null
object. You should do a check on it prior to going into the loop to make sure that it isn't null
.
Solution 2:
I think that, you are not getting valid JSON format from server. before parsing check the response getting from your server, Here is snippet of code, here pass url and get yourReponseInJSONStr check response string in logcat is it in proper JSON format or not and then do the operation of parsing.
try {
HttpClient httpclient = newDefaultHttpClient();
HttpPost httppost = newHttpPost(
url);
List<NameValuePair> nameValuePairs = newArrayList<NameValuePair>(2);
httppost.setEntity(newUrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler = newBasicResponseHandler();
String yourReponseInJSONStr = httpclient.execute(httppost,
responseHandler);
Log.d("yourReponseInJSONStr ", yourReponseInJSONStr );
JSONObject yourJsonObj = newJSONObject(yourReponseInJSONStr);
} catch (Exception e) {
e.printStackTrace();
}
you can also continue parsing in this code as well,Hope this helps you,
Solution 3:
Try this for getting json
publicstatic String getJSONString(String url) {
StringjsonString=null;
HttpURLConnectionlinkConnection=null;
try {
URLlinkurl=newURL(url);
linkConnection = (HttpURLConnection) linkurl.openConnection();
intresponseCode= linkConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStreamlinkinStream= linkConnection.getInputStream();
ByteArrayOutputStreambaos=newByteArrayOutputStream();
intj=0;
while ((j = linkinStream.read()) != -1) {
baos.write(j);
}
byte[] data = baos.toByteArray();
jsonString = newString(data);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (linkConnection != null) {
linkConnection.disconnect();
}
}
return jsonString;
}
publicstaticbooleanisNetworkAvailable(Activity activity) {
ConnectivityManagerconnectivity= (ConnectivityManager) activity
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
returnfalse;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (inti=0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
returntrue;
}
}
}
}
returnfalse;
}
use isNetworkAvailable for checking connection
and i parsed this way
try {
JSONObject jObjectm = newJSONObject(result);
JSONObject jObject=jObjectm.getJSONObject("items");
if(jObject!=null)
{
Iterator<?> iterator1=jObject.keys();
LinkedHashMap<String,LinkedHashMap<String, Object> > inneritem = newLinkedHashMap<String, LinkedHashMap<String, Object> >();
while (iterator1.hasNext() ){
Item hashitem=newItem();
String key1 = (String)iterator1.next();
JSONObject jObject1=jObject.getJSONObject(key1);
Iterator<?> iterator=jObject1.keys();
LinkedHashMap<String, Object> inneritem1 = newLinkedHashMap<String, Object>();
while (iterator.hasNext() ){
String key =(String) iterator.next();
inneritem1.put(key, jObject1.get(key));
}
hashitem.setItem(key1,inneritem1);
inneritem.put(key1,inneritem1);
arrayOfList.add(hashitem);
}
}
} catch (JSONException e) {
System.out.println("NO Json data found");
}
Post a Comment for "Async Android Json Parser Android- Null Pointer Exception"