在Android中解析JSON时出现空指针异常

时间:2013-04-26 12:48:13

标签: android json

我在Android上解析JSON时遇到Null指针异常。

这是我要解析的JSON:

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

以下是我解析它的方式:

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");
        }

    }

这是我得到的堆栈跟踪:

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

你能看到这里可能出现的问题吗?

提前致谢。

1 个答案:

答案 0 :(得分:3)

您传递到result构造函数的String对象JSONArraynull。你永远不会从初始值改变它。