java.lang.RuntimeException:执行doInBackground()时发生错误;引起:java.lang.NullPointerException

时间:2013-06-06 07:48:24

标签: java android

有时我在我的应用程序中遇到此崩溃错误当我多次单击该按钮时,它将崩溃,按钮将SOAP请求发送到服务器,以及以下完整的logcat:

E/AndroidRuntime(16290): FATAL EXCEPTION: AsyncTask #1
06-06 15:17:16.003: E/AndroidRuntime(16290): java.lang.RuntimeException: An error occured while executing doInBackground()
06-06 15:17:16.003: E/AndroidRuntime(16290):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-06 15:17:16.003: E/AndroidRuntime(16290):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
06-06 15:17:16.003: E/AndroidRuntime(16290):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
06-06 15:17:16.003: E/AndroidRuntime(16290):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
06-06 15:17:16.003: E/AndroidRuntime(16290):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
06-06 15:17:16.003: E/AndroidRuntime(16290):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
06-06 15:17:16.003: E/AndroidRuntime(16290):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
06-06 15:17:16.003: E/AndroidRuntime(16290):    at enter code herejava.lang.Thread.run(Thread.java:1027)
06-06 15:17:16.003: E/AndroidRuntime(16290): Caused by: java.lang.NullPointerException
06-06 15:17:16.003: E/AndroidRuntime(16290):    at com.bgi.genebook.task.QueryCalAllDataTask.doInBackground(QueryCalAllDataTask.java:24)
06-06 15:17:16.003: E/AndroidRuntime(16290):    at com.bgi.genebook.task.QueryCalAllDataTask.doInBackground(QueryCalAllDataTask.java:1)
06-06 15:17:16.003: E/AndroidRuntime(16290):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-06 15:17:16.003: E/AndroidRuntime(16290):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
06-06 15:17:16.003: E/AndroidRuntime(16290):    ... 4 more

点击按钮将运行代码:

Map<String, Object> mapSyc = new HashMap<String, Object>();
mapSyc.clear();
QueryCalAllDataTask<VipOxygen> task = new QueryCalAllDataTask<VipOxygen>(
                this);
mapSyc.put("date", date);
mapSyc.put("wsdl", VipBloodOxygenWebService.wsdl);
task.execute(mapSyc);

并且QueryCalAllDataTask.java是:

public class QueryCalAllDataTask<T> extends
        AsyncTask<Map<String, Object>, Integer, List<Map<String, String>>> {
    GBListener<T> listener;

    public QueryCalAllDataTask(GBListener<T> listener) {
        this.listener = listener;
    }

    @Override
    protected synchronized List<Map<String, String>> doInBackground(Map<String, Object>... params) {
        List<Map<String, String>> contentListDay = new ArrayList<Map<String, String>>();
        Log.e("OPERATE", params[0].get("wsdl").toString()); // line 24
        try{
        contentListDay = (List<Map<String, String>>)BaseWebService.getDataAllDaysCountOfMouth(params[0]
                .get("wsdl").toString(), params[0].get("date").toString());
        }catch(Exception e){

        }
        return contentListDay;
    }

    @Override
    protected void onPostExecute(List<Map<String, String>> list) {
        System.out.println("QueryTask.onPostExecute()" + list);
        listener.getAllCalData(list);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        System.out.println(values);
    }

}
谁可以帮助我?为什么?当我单击按钮几次时,这种崩溃偶尔会出现!

2 个答案:

答案 0 :(得分:1)

错误肯定在这里:

contentListDay = (List<Map<String, String>>)BaseWebService.
getDataAllDaysCountOfMouth(params[0].get("wsdl").toString(),  
params[0].get("date").toString());

我不知道get("wsdl")get("date")中的哪一个返回null,无论如何,你不应该在不知道对象是否存在或null的情况下调用对象上的方法

在致电toString(),之前,您应该检查一下。类似的东西:

 something = params[0].get("wsdl");

 if (something==null)
     System.out.println("Can't get parameter wsdl");
 else
     something.toString();

对参数date执行相同的操作,您就可以知道在那里返回null,并修复它。

答案 1 :(得分:0)

代码中的可能操作可能导致NPE异常params[0].get("wsdl").toString()

这意味着,您的第一个参数(索引为0)没有wsdl键。