doInBackground无缘无故抛出NullPointerException



在这里BufferedReaderline = reader.readLine()的代码工作

public class WeatherService extends AsyncTask<TaskParams, Void, String> {
private WeatherServiceCallback callback;
private Exception exception;
public WeatherService(WeatherServiceCallback callback) {
    this.callback = callback;
}
@Override
protected String doInBackground(TaskParams... params) {
    try {
        URL url = new URL("http://api.openweathermap.org/data/2.5/weather?lat=" +
                params[0].getLat() + "&lon=" + params[0].getLon() +
                "&units=" + TaskParams.getUnits() +
                "&type=" + TaskParams.getAccuracy() + "&lang=" + TaskParams.getLanguage() +
                "&appid=10660a09a9fb335d72f576f7aa1bbe5b");
        URLConnection connection = url.openConnection();
        InputStream inputStream = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        return builder.toString();
    } catch (MalformedURLException e) {
        exception = e;
    } catch (IOException e) {
        exception = e;
    }
    return null;
}
@Override
protected void onPostExecute(String s)
{
    if (s == null && exception != null)
    {
        callback.serviceFailure(exception);
        return;
    }
    try
    {
        JSONObject data = new JSONObject(s);
        Parameters parameters = new Parameters();
        parameters.poopulate(data);
        callback.serviceSuccess(parameters);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
}

我将代码复制粘贴到其他类,因为它具有非常相似的功能,现在没有理由我在while ((line = reader.readLine()) != null)中获得NullPointerException,我不知道为什么,因为我说它是复制粘贴的(我只更改了URL和对象返回如果服务成功)

public class PollutionService extends AsyncTask<TaskParams, Void, String>
{
private PollutionServiceCallback callback;
private Exception exception;
private URLConnection connection;
private InputStream inputStream;
private InputStreamReader streamReader;
private BufferedReader reader;
public PollutionService(PollutionServiceCallback callback) {
    this.callback = callback;
}
@Override
protected String doInBackground(TaskParams... params) {
    try
    {
        URL url = new URL("http://api.openweathermap.org/pollution/v1/co/" + params[0].getLat() +
                "," + params[0].getLon() + "/current.json?&appid=10660a09a9fb335d72f576f7aa1bbe5b");
        try
        {
            connection = url.openConnection();
        }
        catch (IOException e)
        {
            exception = new Exception("Connection error");
        }
        try
        {
            inputStream = connection.getInputStream();
        }
        catch (IOException e)
        {
            exception = new Exception("Input stream error");
        }
        try
        {
            streamReader = new InputStreamReader(inputStream);
        }
        catch (NullPointerException e)
        {
            exception = new Exception("Input stream reader error");
        }
        try
        {
            reader = new BufferedReader(streamReader);
        }
        catch (NullPointerException e)
        {
            exception = new Exception("Buffered reader error");
        }
        StringBuilder builder = new StringBuilder();
        String line;
        try
        {
            while ((line = reader.readLine()) != null)
            {
                builder.append(line);
            }
        }
        catch (IOException e)
        {
            exception = e;
        }
        return builder.toString();
    }
    catch (MalformedURLException e)
    {
        exception = e;
    }
    return null;
}
@Override
protected void onPostExecute(String s)
{
    if (s == null && exception != null)
    {
        callback.pollutionServiceFailure(exception);
        return;
    }
    try
    {
        JSONObject data = new JSONObject(s);
        PollutionParameters parameters = new PollutionParameters();
        parameters.poopulate(data);
        callback.pollutionServiceSuccess(parameters);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
}

什么线索吗?

编辑这是为PollutionActivity重写的代码。回调函数serviceFailure现在打印我手机屏幕上的URL地址

public class PollutionService extends AsyncTask<TaskParams, Void, String>
{
private PollutionServiceCallback callback;
private Exception exception;
public PollutionService(PollutionServiceCallback callback) {
    this.callback = callback;
}
@Override
protected String doInBackground(TaskParams... params) {
    try
    {
        URL url = new URL("http://api.openweathermap.org/pollution/v1/co/" + params[0].getLat() +
                "," + params[0].getLon() + "/current.json?&appid=10660a09a9fb335d72f576f7aa1bbe5b");
        URLConnection connection = url.openConnection();
        InputStream inputStream = connection.getInputStream();
        InputStreamReader streamReader = new InputStreamReader(inputStream);
        BufferedReader reader = new BufferedReader(streamReader);
        StringBuilder builder = new StringBuilder();
        String line;
        try
        {
            while ((line = reader.readLine()) != null)
            {
                builder.append(line);
            }
        }
        catch (IOException e)
        {
            exception = e;
        }
        return builder.toString();
    }
    catch (MalformedURLException e)
    {
        exception = e;
    }
    catch (IOException e)
    {
        exception = e;
    }
    return null;
}
@Override
protected void onPostExecute(String s)
{
    if (s == null && exception != null)
    {
        callback.pollutionServiceFailure(exception);
        return;
    }
    try
    {
        JSONObject data = new JSONObject(s);
        PollutionParameters parameters = new PollutionParameters();
        parameters.poopulate(data);
        callback.pollutionServiceSuccess(parameters);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
}

调试显示,代码跳转到异常后InputStream inputStream = connection.getInputStream();

如果你为reader获得NPE,那么这意味着reader没有被初始化并为空。您的代码在第23行和第89行崩溃。所以我相信问题是在开始的某个地方而不是在点本身,可能是某个对象变为null,比如connection。因为这里没有显示行号。检查每个对象的空指针,如if (connection!=null)。由于初始数据为空,可能是输入流或其他数据,因此您的阅读器对象没有初始化。还要检查您是否在调试中为每个对象获取值。

相关内容

  • 没有找到相关文章

最新更新