2016-01-14 36 views
0

我是Android開發的初學者。我不斷收到Caused by: java.lang.NullPointerException錯誤。我已經使用optJSONObject(0)方法和if語句results_object.has("formatted_address")。有沒有辦法忽略錯誤異常?如何在java中使用JSON has和isNull方法?

public String convertLatLngToAddress(JSONObject lat_lng) throws JSONException { 
    Double lat = lat_lng.getDouble("lat"); 
    Double lng = lat_lng.getDouble("lng"); 

    String link = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + lng + "&sensor=false"; 
    String address_in_string = urlToStringBuffer(link); 
    JSONObject address_object = new JSONObject(address_in_string); 
    JSONArray results = address_object.optJSONArray("results"); 
    JSONObject results_object = results.optJSONObject(0); 
    if(results_object.has("formatted_address") && !results_object.isNull("formatted_address")) { // 
     String address = results_object.optString("formatted_address"); 
     return address; 
    } else { 
     return ""; 
    } 
}  

JSON

{ 
    "results" : [ 
     { 
     "address_components" : [ 
      { 
       "long_name" : "1600", 
       "short_name" : "1600", 
       "types" : [ "street_number" ] 
      }, 
      { 
       "long_name" : "Amphitheatre Pkwy", 
       "short_name" : "Amphitheatre Pkwy", 
       "types" : [ "route" ] 
      }, 
      { 
       "long_name" : "Mountain View", 
       "short_name" : "Mountain View", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "Santa Clara County", 
       "short_name" : "Santa Clara County", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "California", 
       "short_name" : "CA", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "United States", 
       "short_name" : "US", 
       "types" : [ "country", "political" ] 
      }, 
      { 
       "long_name" : "94043", 
       "short_name" : "94043", 
       "types" : [ "postal_code" ] 
      } 
     ], 
     "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA", 
     "geometry" : { 
      "location" : { 
       "lat" : 37.4224764, 
       "lng" : -122.0842499 
      }, 
      "location_type" : "ROOFTOP", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 37.4238253802915, 
        "lng" : -122.0829009197085 
       }, 
       "southwest" : { 
        "lat" : 37.4211274197085, 
        "lng" : -122.0855988802915 
       } 
      } 
     }, 
     "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA", 
     "types" : [ "street_address" ] 
     } 
    ], 
    "status" : "OK" 
} 

日誌

01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 W/dalvikvm: threadid=10: thread exiting with uncaught exception (group=0xa615e908) 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground() 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at android.os.AsyncTask$3.done(AsyncTask.java:299) 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at java.util.concurrent.FutureTask.run(FutureTask.java:239) 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at java.lang.Thread.run(Thread.java:856) 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime: Caused by: java.lang.NullPointerException 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at com.example.peter.fypv1.Model.Route.convertLatLngToAddress(Route.java:114) 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at com.example.peter.fypv1.MainActivity$JSONTask.doInBackground(MainActivity.java:182) 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at com.example.peter.fypv1.MainActivity$JSONTask.doInBackground(MainActivity.java:100) 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at android.os.AsyncTask$2.call(AsyncTask.java:287) 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)  
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)  
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)  
01-14 17:35:40.227 6249-6272/com.example.peter.fypv1 E/AndroidRuntime:  at java.lang.Thread.run(Thread.java:856)  
01-14 17:35:41.755 6249-6272/? I/Process: Sending signal. PID: 6249 SIG: 9 
+0

看來'JSONObject results_object = results.optJSONObject(0);'返回null,因此你不能在空對象上調用「has」。 –

+0

@Thomas R.非常感謝你,你是對的,我已經超過了這個API的速率限制。所以json數組是不同的。 –

回答

1
JSONObject results_object = results.optJSONObject(0); 

你永遠不會檢查數組的大小類似這樣的以往任何時候都得到數組項:當你想通過結果的所有項目

JSONArray results = address_object.optJSONArray("results"); 
if(results.length()>0){ 
    JSONObject results_object = results.optJSONObject(0); 
} 

JSONArray results = address_object.optJSONArray("results"); 
for(int i=0;i<results.length() ;i++){ 
    JSONObject results_object = results.optJSONObject(i); 
} 
0

從文檔here

我會說,你

results.optJSONObject(0) 

返回null。由於參數是String你可能想要做

results.optJSONObject("results") 

然而,這可能不會返回JSONObject(見文檔),但JSONArray這也將導致NullPointerException。要獲得陣列使用optJSONArray(String key)

而且由於null已返回,因此無法對結果執行has()

1

這就是你應該寫:

public String convertLatLngToAddress(JSONObject lat_lng) throws JSONException { 
     Double lat = lat_lng.getDouble("lat"); 
     Double lng = lat_lng.getDouble("lng"); 

     String link = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + lng + "&sensor=false"; 
     String address_in_string = urlToStringBuffer(link); 
     JSONObject address_object = new JSONObject(address_in_string); 
     JSONArray results = address_object.optJSONArray("results"); 
     JSONObject results_object = results.optJSONObject(0); 
     if(results_object != null) { 
      if(results_object.has("formatted_address") && !results_object.isNull("formatted_address")) { // 
       String address = results_object.optString("formatted_address"); 
       return address; 
      } 
     } 
    } 

你首先要檢查對象是否不等於null,然後調用任何方法。

我希望這將解決您的問題:)

2

使用下面的代碼:

JSONArray results = address_object.optJSONArray("results"); 
//then get the all objects insid this array 
for(int i=0;i<results.length;i++){ 
    JsonObjects jsonObjects=results.getJsonObjects(i); 
    // then access all child data using this jsonObject} 

您還可以使用此方法有一樣(「參數名」)和ISNULL(「參數-name「)進行驗證。

在使用前務必檢查JsonArray的長度。

希望它會幫助你!

相關問題