4

I am getting a null pointer exception while handling JSON data, i have checked that JSON which i am getting is correct. further i want to store this data in hash map and retrive it one by one as per the requirements.

    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)

If any other part of Code is required let me know.

4
  • what part of the code is line 113 of app.java Commented Aug 23, 2013 at 12:13
  • pDialognew.dismiss(); // this is line 113 Commented Aug 23, 2013 at 12:19
  • seems pDialognew is not an object, is sometihng else accessing/dismissing/destroying it before postexecute executes Commented Aug 23, 2013 at 12:22
  • why is this tagged as php? Commented Aug 23, 2013 at 12:25

3 Answers 3

1

Normally AsyncTask is used as new thread to perform heavy operations without blocking main UI thread.But, you have used the code which i mentioned below inside doInBackground() of AsyncTask, which will again make the process to be performed in main thread(especially in higher version mobiles), blocking it. so, better remove the following lines and try running it..

runOnUiThread(new Runnable() {
        @Override
        public void run() {

}
};
Sign up to request clarification or add additional context in comments.

Comments

0

An AsyncTask has several parts: a doInBackground method that does, in fact, run on a separate thread, and a onPostExecute method that runs on the UI thread. The purpose of onPostExecute is to publish results (such as updating the view hierarchy, or setting text in a text view) that must be done on the UI thread. It also can post progress updates. In order for this to all work properly, the AsyncTask must be created, and the execute method called, on the UI thread.

You must not call UI actions from within doInBackground -- doing so will crash your application.

1 Comment

I am grateful for your response, but if can also point out my mistake in the code above i will be grateful. what i have done is just to get data and put in a hashmap, and i have been doing the same for quite a long time.
0

First of all, move the declaration of pDialognew into your SearchData class.

Second, as Hari pointed out, get rid off the runOnUiThread stuff. Your doInBackground() method should look about like so:

protected String doInBackground(String... params) {
    try {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // ... rest of your code
    } catch (JSONException e) {
        Log.i("Error", e.toString());
    }
    return null;
}

Third, in your onPostExecute() method, ensure, pDialognew is not null (altough I have no idea how it can be null here):

if (pDialognew != null) pDialognew.dismiss();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.