我在处理 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)
如果需要代码的任何其他部分,请告诉我。