简体   繁体   中英

Null pointer exception while parsing JSON in Android

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM