0

I have an async task in my class and it is is about parsing JSON. But when I execute the code, in LogCat I see the message: FATAL EXCEPTION: AsyncTask #1

.

.

.

Caused by: java.lang.NullPointerException.

If you want to look at it, here is my code:

MainActivity.java

    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         new ReadJSON().execute(there is an address here);

         textView = (TextView) findViewById(R.id.textView1);
   }

   //async task:
    InputStream inputStream = null;
    String result = null;
    HttpResponse response;
    BufferedReader reader;
    JSONObject jObject;
    String aJsonString1;
    String aJsonString2;


    public class ReadJSON extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... url) {

            DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost(there is the same address here);
            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");

            try {
                response = httpclient.execute(httppost);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                    e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }           
            HttpEntity entity = response.getEntity();

            try {
                inputStream = entity.getContent();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // json is UTF-8 by default i beleive

            try {
                reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            StringBuilder sb = new StringBuilder();

            String line = null;
            try {
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            result = sb.toString();



            try {
                jObject = new JSONObject(result);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                aJsonString1 = jObject.getString("value");
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                aJsonString2 = jObject.getString("timestamp");
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            return aJsonString1;

        }

            protected void onPostExecute(String result) {
                textView.setText(aJsonString1);


            }

    }  

Can you see the problem here? Anyone have an idea?

Thanks in advance

Edit: Here is the LogCat:

   04-25 16:27:06.851: W/System.err(4697):  at     android.os.AsyncTask$2.call(AsyncTask.java:287)
   04-25 16:27:06.861: W/System.err(4697):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
   04-25 16:27:06.861: W/System.err(4697):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
   04-25 16:27:06.871: W/System.err(4697):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
   04-25 16:27:06.871: W/System.err(4697):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
   04-25 16:27:06.881: W/System.err(4697):  at java.lang.Thread.run(Thread.java:856)
   04-25 16:27:06.891: W/dalvikvm(4697): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
   04-25 16:27:06.911: E/AndroidRuntime(4697): FATAL EXCEPTION: AsyncTask #1
   04-25 16:27:06.911: E/AndroidRuntime(4697): java.lang.RuntimeException: An error occured while executing doInBackground()
   04-25 16:27:06.911: E/AndroidRuntime(4697):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
   04-25 16:27:06.911: E/AndroidRuntime(4697):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
   04-25 16:27:06.911: E/AndroidRuntime(4697):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
   04-25 16:27:06.911: E/AndroidRuntime(4697):  at java.util.concurrent.FutureTask.run(FutureTask.java:239)
   04-25 16:27:06.911: E/AndroidRuntime(4697):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
   04-25 16:27:06.911: E/AndroidRuntime(4697):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
   04-25 16:27:06.911: E/AndroidRuntime(4697):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
   04-25 16:27:06.911: E/AndroidRuntime(4697):  at java.lang.Thread.run(Thread.java:856)
   04-25 16:27:06.911: E/AndroidRuntime(4697): Caused by: java.lang.NullPointerException
   04-25 16:27:06.911: E/AndroidRuntime(4697):  at com.example.ayasms.MainActivity$ReadJSON.doInBackground(MainActivity.java:189)
   04-25 16:27:06.911: E/AndroidRuntime(4697):  at com.example.ayasms.MainActivity$ReadJSON.doInBackground(MainActivity.java:1)
   04-25 16:27:06.911: E/AndroidRuntime(4697):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
   04-25 16:27:06.911: E/AndroidRuntime(4697):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
   04-25 16:27:06.911: E/AndroidRuntime(4697):  ... 4 more 
13
  • 2
    Post the complete stack trace Commented Apr 25, 2013 at 13:22
  • You shouldn't hide all exceptions! If an exception is thrown, you cannot continue as if nothing happened. Commented Apr 25, 2013 at 13:25
  • You can copy by clicking in the log, select all, click save icon, then copy/paste log here Commented Apr 25, 2013 at 13:26
  • 2
    Heh. You look at the code in way more detail than I do @codeMagic. I just look over it for the current error :-) Commented Apr 25, 2013 at 13:29
  • 1
    There's nothing wrong with catching them...it's not doing anything if it throws an exception that can get you in trouble Commented Apr 25, 2013 at 13:34

2 Answers 2

2

I assume that your try catch error handling inside the AsyncJob is the problem.

For example if the line httpclient.execute(httppost);thrown an exception you catch it and print it to Stderr (which is partly visible in the LogCat) but then you continue!

Of course without having executed httpclient.execute(..) you have no response and therefore you are getting a NullPointerException.

Conclusion: Pack everything inside the AsyncJob in one try catch environment. Furthermore you should investigate which class fails first and find out why.

try {
    response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    inputStream = entity.getContent();
    reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
    StringBuilder sb = new StringBuilder();

    String line = null;
    while ((line = reader.readLine()) != null) {
    sb.append(line + "\n");
    }
    result = sb.toString();
    jObject = new JSONObject(result);  
    aJsonString1 = jObject.getString("value");
    aJsonString2 = jObject.getString("timestamp");
    return aJsonString1;
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (IllegalStateException e) {
    e.printStackTrace();
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();  
} catch (JSONException e) {
    e.printStackTrace();
}           
return null; // Error case
Sign up to request clarification or add additional context in comments.

2 Comments

How can we pack everything in one try catch?
See my edited question. Anyway you should learn the basics of Java try/catching of Exceptions. Just performing the Eclipse quick-fix isn't sustainable.
1

On this line:

textView.setText(aJsonString1);

textView might still be null.

So, in on your onCreate method switch the following lines:

new ReadJSON().execute(there is an address here);

textView = (TextView) findViewById(R.id.textView1);

to

textView = (TextView) findViewById(R.id.textView1);
new ReadJSON().execute(there is an address here);

1 Comment

Thanks, I did them but the same problem is still there

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.