2013-04-25 64 views
0

我在我的類中有一個異步任務,它是關於解析JSON。但是當我執行代碼時,在LogCat中,我看到以下消息:FATAL EXCEPTION:AsyncTask#1使用異步任務時Android空指針異常

引起:java.lang.NullPointerException。

如果你想看看吧,這裏是我的代碼:

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


      } 

    } 

你可以看到這裏的問題?任何人有想法?

在此先感謝

編輯:這裏是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 
+2

發佈完整堆棧跟蹤 – 2013-04-25 13:22:50

+0

您不應該隱藏所有異常!如果拋出異常,就不會像沒有任何事情發生一樣繼續。 – BobTheBuilder 2013-04-25 13:25:47

+0

您可以點擊日誌,選擇全部,點擊保存圖標,然後在這裏複製/粘貼日誌 – codeMagic 2013-04-25 13:26:48

回答

2

我假設你嘗試捕捉錯誤AsyncJob內處理的問題。

例如,如果行httpclient.execute(httppost);拋出一個異常,您將其捕獲並將其打印到Stderr(在LogCat中可以部分顯示),但是您繼續!

當然,沒有執行httpclient.execute(..)你沒有任何反應,因此你會得到一個NullPointerException。

結論:在一個try catch環境中打包AsyncJob中的所有內容。此外,你應該調查哪個班先失敗並找出原因。

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 
+0

我們怎樣才能收拾一切? – 2013-04-25 13:44:06

+0

看到我編輯的問題。無論如何,你應該學習Java嘗試/捕獲異常的基礎知識。只是執行Eclipse快速修復是不可持續的。 – Robert 2013-04-25 13:54:07

+0

謝謝youuuuuuuuuuuuuuuu – 2013-04-25 14:00:21

1

在此行中:

textView.setText(aJsonString1); 

textView仍可能爲空。

所以,在你onCreate方法切換以下行:

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

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

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

謝謝,我做了他們,但同樣的問題仍然存在 – 2013-04-25 13:42:09