我是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
看來'JSONObject results_object = results.optJSONObject(0);'返回null,因此你不能在空對象上調用「has」。 –
@Thomas R.非常感謝你,你是對的,我已經超過了這個API的速率限制。所以json數組是不同的。 –