异步任务中的异常

时间:2019-04-15 14:07:35

标签: android android-asynctask jackson

What is a NullPointerException, and how do I fix it?我在提到有关堆栈溢出中空指针异常的许多答案之后问这个问题。我尝试了很多方法但都失败了。

所以问题是我在异步任务中遇到了空指针异常。我要留下负责此错误的代码。

下面是我遇到错误的班级。

GetArrayAsyncTask:

@TargetApi(19)
public Map doInBackground(String... strArr) {
    StringBuffer stringBuffer = new StringBuffer();
    String str = null;
    try {
        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(strArr[0]).openConnection();
        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.connect();
        InputStream inputStream = httpURLConnection.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        while (true) {
            String readLine = bufferedReader.readLine();
            if (readLine == null) {
                break;
            }
            stringBuffer.append(readLine);
        }
        bufferedReader.close();
        inputStreamReader.close();
        inputStream.close();
        httpURLConnection.disconnect();
        String stringBuffer2 = stringBuffer.toString();
        if (!stringBuffer2.contains("host") && !stringBuffer2.contains("version")) {
            boolean contains = stringBuffer2.contains("http");
        }
        List list = JsonHelper.toList(new JSONArray(stringBuffer2));
        HashMap hashMap = new HashMap();
        hashMap.put("data", list);
        return hashMap;
    } catch (MalformedURLException unused) {
    } catch (ProtocolException unused2) {
    } catch (IOException unused3) {
    } catch (JSONException unused) {
        return (Map) JsonUtils.stringObject(str, Map.class); //Error is here.
    }
    return null;
}

public void onPostExecute(Map map) {
    if (map == null || map.size() <= 0) {
        Toast.makeText(this.context, "Data format error!", Toast.LENGTH_LONG).show();
        this.listener.onGetArrayResultsData(map, this.request_code);
        return;
    }
    this.listener.onGetArrayResultsData(map, this.request_code);
}

该异常发生在return (Map) JsonUtils.stringObject(str, Map.class);

JsonUtils:

public class JsonUtils {
public static String jsonString(Object obj) {
    try {
        return new ObjectMapper().writeValueAsString(obj);
    } catch (JsonProcessingException unused) {
        return null;
    }
}

public static <T> T stringObject(String str, Class<T> cls) {
    try {
        return new ObjectMapper().readValue(str, cls); //Error line.
    } catch (JsonParseException unused) {
        return null;
    } catch (JsonMappingException unused2) {
        return null;
    } catch (IOException unused3) {
        return null;
    }
}
}

LogCat:

java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:318)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:760)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
    at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:889)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
    at converter.JsonUtils.stringObject(JsonUtils.java:20)
    at com.kds.gold.pi.asyncs.GetArrayAsyncTask.doInBackground(GetArrayAsyncTask.java:111)
    at com.kds.gold.pi.asyncs.GetArrayAsyncTask.doInBackground(GetArrayAsyncTask.java:24)

该项目正在使用Jackson更快的XML库。

编辑:  经过一些修改后,我遇到了一个奇怪的问题,即在用“”而不是null(this actually solved null pointer exception but led to functional issue)初始化str之后。当我向该行添加断点(return (Map) JsonUtils.stringObject(str, Map.class); //Error is here)并进行调试时,该应用程序可以正常运行。而当我通过正常模式运行它时,它会按照onPost execute方法中的定义提供Data format error!吐司。

1 个答案:

答案 0 :(得分:1)

return (Map) JsonUtils.stringObject(str, Map.class); //Error is here.

strnull。您似乎从未为其分配值。并且,由于此行代码位于catch块中,因此无论如何在为str分配值之前都会发生异常。