我正在做一个显示附近地方的应用程序。但我有一个运行时错误FATAL EXCEPTION: AsyncTask #2NullPointerException
和应用程序崩溃。
我正在尝试发送附近地点数据URL。但我不明白为什么会这样。
这是日志猫:
FATAL EXCEPTION: AsyncTask #2
Process: com.example.findnearbyfinder, PID: 20784
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$4.done(AsyncTask.java:415)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.InputStream.close()' on a null object reference
at com.example.findnearbyfinder.DownloadUrl.retireveUrl(DownloadUrl.java:42)
at com.example.findnearbyfinder.FetchData.doInBackground(FetchData.java:58)
at com.example.findnearbyfinder.FetchData.doInBackground(FetchData.java:16)
at android.os.AsyncTask$3.call(AsyncTask.java:394)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
Downloadurl.java
:
package com.example.findnearbyfinder;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadUrl {
public String retireveUrl(String url)throws IOException{
String urlData = "";
HttpURLConnection httpURLConnection = null;
InputStream inputStream = null;
try{
URL getUrl = new URL(url);
httpURLConnection = (HttpURLConnection) getUrl.openConnection();
httpURLConnection.connect();
inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader((new InputStreamReader(inputStream)));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = bufferedReader.readLine())!=null){
sb.append(line);
}
urlData = sb.toString();
bufferedReader.close();
}catch (Exception e){
Log.d("Exception", e.toString());
}finally {
inputStream.close();
httpURLConnection.disconnect();
}
return urlData;
}
}
FetchData.java :
package com.example.findnearbyfinder;
import android.os.AsyncTask;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
public class FetchData extends AsyncTask<Object,String,String> {
String googleNearbyPlaceData;
GoogleMap googleMap;
String url;
@Override
protected void onPostExecute(String s) {
try{
JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArray= jsonObject.getJSONArray("result");
for (int i=0 ; i<jsonArray.length(); i++){
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
JSONObject getLocation = jsonObject1.getJSONObject("geometry")
.getJSONObject("location");
String lat = getLocation.getString("lat");
String lng = getLocation.getString("lng");
JSONArray getName = jsonArray.getJSONArray(i);
String name = getName.getString(Integer.parseInt("name"));
LatLng latLng = new LatLng(Double.parseDouble(lat),Double.parseDouble(lng));
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.title(name);
markerOptions.position(latLng);
googleMap.addMarker(markerOptions);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,15));
}
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
@Override
protected String doInBackground(Object... objects) {
try{
googleMap = (GoogleMap) objects[0];
url = (String) objects[1];
DownloadUrl downloadUrl = new DownloadUrl();
googleNearbyPlaceData = downloadUrl.retireveUrl(url);
}catch(IOException e){
e.printStackTrace();
}
return googleNearbyPlaceData;
}
}
我期待下载附近的地方URL并获取URL,但由于NullPointerException而崩溃。
1条答案
按热度按时间wljmcqd81#
1.您首先需要弄清楚为什么
inputStream
是null
(我假设在try
-block中也抛出了一个异常,但您没有提供此代码)1.要解决这个方法,你可以用空值检查来包围这个语句:
但是我建议你弄清楚为什么
inputStream
是null
并解决这个问题。