在處理JSON數據時,我得到一個空指針異常,我檢查了我所得到的JSON是正確的。更進一步,我想將這些數據存儲在哈希映射表中,並根據需要一一查找。異步任務中的空指針錯誤
public ProgressDialog pDialognew;
static JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> allValueList;
class SearchData extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialognew = new ProgressDialog(Activity.this);
pDialognew.setMessage("Loading. Please wait...");
pDialognew.setIndeterminate(false);
pDialognew.setCancelable(true);
pDialognew.show();
}
@Override
protected String doInBackground(String... params) {
runOnUiThread(new Runnable() {
@Override
public void run() {
int success;
try
{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("match",pName));
JSONObject json = jParser.makeHttpRequest(GlobalServerLocation.url_pol, "GET", params);
success = json.getInt("success");
if (success == 1)
{
JSONArray productObj = json.getJSONArray("object"); // JSON Array
Log.i("val", productObj.toString()); // Correct JSON
total = productObj.length(); // Correct number of rows
Log.i("Len", String.valueOf(total));
for (int j = 0; j < productObj.length(); j++)
{
JSONObject productnew = productObj.getJSONObject(j);
String LS = productnew.getString(TAG_LS);
String CN = productnew.getString(TAG_CN);
String SN = productnew.getString(TAG_SN);
String PN = productnew.getString(TAG_PN);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_LS, LS);
map.put(TAG_CN, CN);
map.put(TAG_SN, SN);
map.put(TAG_PN, PN);
allValueList.add(map);
}
}
else
{
Log.i("Error", " Some Error from Php Script");
}
}
catch (JSONException e)
{
Log.i("Error", e.toString());
}
}
});
return null;
}
@Override
protected void onPostExecute(String file_url) {
pDialognew.dismiss(); // Line 113
}
}
08-23 17:17:33.449: E/AndroidRuntime(31930): FATAL EXCEPTION: main
08-23 17:17:33.449: E/AndroidRuntime(31930): java.lang.NullPointerException
08-23 17:17:33.449: E/AndroidRuntime(31930): at my.india.our.app$SearchData$1.run(app.java:113)
08-23 17:17:33.449: E/AndroidRuntime(31930): at android.os.Handler.handleCallback(Handler.java:587)
08-23 17:17:33.449: E/AndroidRuntime(31930): at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 17:17:33.449: E/AndroidRuntime(31930): at android.os.Looper.loop(Looper.java:123)
08-23 17:17:33.449: E/AndroidRuntime(31930): at android.app.ActivityThread.main(ActivityThread.java:3729)
08-23 17:17:33.449: E/AndroidRuntime(31930): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 17:17:33.449: E/AndroidRuntime(31930): at java.lang.reflect.Method.invoke(Method.java:507)
08-23 17:17:33.449: E/AndroidRuntime(31930): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
08-23 17:17:33.449: E/AndroidRuntime(31930): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
08-23 17:17:33.449: E/AndroidRuntime(31930): at dalvik.system.NativeStart.main(Native Method)
如果需要代碼的任何其他部分讓我知道。
什麼部分代碼是app.java的第113行 –
pDialognew.dismiss(); //這是第113行 – user2327124
似乎'pDialognew'不是一個對象,有些人在執行postexecute之前訪問/解僱/銷燬它 –