[英]how to solve getting null pointer exception in JSON parsing?
我正在開發一個從json解析中檢索數據的應用程序。 我在執行異步任務期間收到空指針異常。
請給我任何建議我做什么。
庇護班在這里
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(ContactListActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
JSONParser sh = new JSONParser();
// Making a request to url and getting response
JSONObject jsonStr = sh.getJSONFromUrl(url);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
// Getting JSON Array node
contacts = jsonStr.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String name = c.getString(TAG_NAME);
String mobile = c.getString(TAG_PHONE_MOBILE);
ContactBean bean=new ContactBean();
bean.setName(name);
bean.setPhoneNo(mobile);
contactList.add(bean);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ContanctAdapter adapter = new ContanctAdapter(
ContactListActivity.this, R.layout.alluser_row, contactList);
lv.setAdapter(adapter);
}
並從此方法獲取json數據
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
其中可能有很多語句可能導致NullPointer。 正如評論所暗示的那樣,沒有痕跡很難識別。 但是最可能的地方是
// Getting JSON Array node
contacts = jsonStr.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
...
}
可能沒有TAG-CONTACTS
json數組,並且contacts對象可能為null ..並且在循環中,您嘗試獲取長度。 如果對象為null,則for循環可能會導致NPE。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.