0

I am getting Null pointer exception while parsing JSON on Android.

This is the JSON I want to parse:

[{"value":"91.84","timestamp":"2012-10-11 14:12:13"}]

Here is the way I am parsing it:

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("url is written here");
        // Depends on your web service
        httppost.setHeader("Content-type", "application/json");
        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");
            }


        JSONArray jsonArray = new JSONArray(result);

         for (int i=0; i < jsonArray.length(); i++)
            {
                JSONObject oneObject = jsonArray.getJSONObject(i);
                // Pulling items from the array
                String oneObjectsItem = oneObject.getString("value");
                String oneObjectsItem2 = oneObject.getString("timestamp");
            }

         aJsonString1  = jsonArray.getJSONObject(0).getString("value");
         aJsonString2  = jsonArray.getJSONObject(0).getString("timestamp");


        return aJsonString1;

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("JSON",e.getMessage() + "  " + e);
        }           
        return null; // Error case

    }

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

        if(aJsonString1==null){
            textView.setText("nothing to show");
        }

    }

And this is the stack trace I get:

04-26 15:37:40.930: E/AndroidRuntime(1387): FATAL EXCEPTION: AsyncTask #1
04-26 15:37:40.930: E/AndroidRuntime(1387): java.lang.RuntimeException: An error occured while executing doInBackground()
04-26 15:37:40.930: E/AndroidRuntime(1387):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-26 15:37:40.930: E/AndroidRuntime(1387):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
04-26 15:37:40.930: E/AndroidRuntime(1387):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
04-26 15:37:40.930: E/AndroidRuntime(1387):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
04-26 15:37:40.930: E/AndroidRuntime(1387):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-26 15:37:40.930: E/AndroidRuntime(1387):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-26 15:37:40.930: E/AndroidRuntime(1387):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-26 15:37:40.930: E/AndroidRuntime(1387):     at java.lang.Thread.run(Thread.java:856)
04-26 15:37:40.930: E/AndroidRuntime(1387): Caused by: java.lang.NullPointerException
04-26 15:37:40.930: E/AndroidRuntime(1387):     at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
04-26 15:37:40.930: E/AndroidRuntime(1387):     at org.json.JSONTokener.nextValue(JSONTokener.java:94)
04-26 15:37:40.930: E/AndroidRuntime(1387):     at org.json.JSONArray.<init>(JSONArray.java:87)
04-26 15:37:40.930: E/AndroidRuntime(1387):     at org.json.JSONArray.<init>(JSONArray.java:103)
04-26 15:37:40.930: E/AndroidRuntime(1387):     at com.example.ayasms.MainActivity$ReadJSON.doInBackground(MainActivity.java:176)
04-26 15:37:40.930: E/AndroidRuntime(1387):     at com.example.ayasms.MainActivity$ReadJSON.doInBackground(MainActivity.java:1)
04-26 15:37:40.930: E/AndroidRuntime(1387):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-26 15:37:40.930: E/AndroidRuntime(1387):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-26 15:37:40.930: E/AndroidRuntime(1387):     ... 4 more

Can you see what might be the problem here?

Thanks in advance.

6
  • The first idea that comes to my mind is that I'd like you to write code which checks for the success of operations which can fail, and output a respective log message if they fail. ;) Commented Apr 26, 2013 at 12:51
  • Don't debug nullpointers by posting here :p You can always figure it out yourself if you learn how. First thing to check is the line number. Commented Apr 26, 2013 at 12:51
  • OMG why did you just copy pasted previous answer? Its not result its sb.toString() in your code Commented Apr 26, 2013 at 12:55
  • There are lots of line numbers here. Which one I should look at? Commented Apr 26, 2013 at 12:56
  • hehe=D @Stan, you are right I am too much reckless, my pardon. Thanks for all answers. Commented Apr 26, 2013 at 13:01

1 Answer 1

3

The String object result, which you're passing into the JSONArray constructor, is null. You are never changing it from its initial value.

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

3 Comments

Thats right careful eyes here! I should pass "response" into JSONArray constructor right?
I think you probably want to pass in sb.toString(), but really you know more than I do about what it should be doing!
No! Cause you have 19.7k rep and I have 5! I am a newbei, so actually you can know more about my code than me:) Thanks for your help.

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.