Android运行时:致命异常:AsyncTask #2

AndroidRuntime: FATAL EXCEPTION: AsyncTask #2

提问人:Yash Deshmukh 提问时间:5/9/2023 最后编辑:rgettmanYash Deshmukh 更新时间:5/10/2023 访问量:63

问:

我正在制作一个用于显示附近地点的应用程序。但是我有一个运行时错误
,应用程序崩溃了。
我正在尝试发送附近的地点数据 URL。但我不明白为什么会这样。
FATAL EXCEPTION: AsyncTask #2NullPointerException

这是日志猫:

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,它崩溃了。

java android nullpointerexception android-asynctask

评论

0赞 Lakshmi 5/10/2023
显示您在哪里/如何进行异步调用。

答:

0赞 Mikhail Guliaev 5/9/2023 #1
  1. 您首先需要弄清楚为什么是(我想在-block中早些时候也抛出了一个异常,但您没有提供此代码)inputStreamnulltry

  2. 要解决此方法,您可以通过 null 检查来包围此语句:

     if (inputStream != null) {
         inputStream.close();
     }
    

但我建议你弄清楚原因并解决这个问题。inputStreamnull