我是android开发的初学者。我一直收到Caused by: java.lang.NullPointerException
错误。我已经使用了optJSONObject(0)
方法和if语句results_object.has("formatted_address")
。有没有办法忽略错误异常?
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 :(得分: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}
你也可以在这里使用像has(&#34; param-name&#34;)和isNull(&#34; param-name&#34;)这样的方法进行验证。
在使用之前,请务必检查JsonArray的长度。
希望它会对你有帮助!
答案 1 :(得分: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);
}
答案 2 :(得分: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,因为它调用了任何方法。
我希望这能解决你的问题:)
答案 3 :(得分:0)
来自文档here
我会说你的
results.optJSONObject(0)
返回null。由于参数是您可能想要的String
results.optJSONObject("results")
但是,这可能不会返回JSONObject
(请参阅文档),而是JSONArray
,这也会产生NullPointerException
。要使用optJSONArray(String key)
来获取数组。
由于返回null
,您无法对结果进行has()
。