在Timer.Schedule()中抛出空指针异常;

时间:2012-06-15 07:45:56

标签: java android multithreading timer timertask

在以下代码中 t.schedule(timertask,d.getDate(),1000); 正在投掷 NullPointer异常 帮助我

目标:
我的基本目标是运行一种方法(每次在固定的时间间隔之后)将发送一些数据到我的android的 webservice设备

Date d = new Date();
    d.getDate();
    timertask = new TimerTask() {
        @Override
        public void run() {
            new Thread() {

                public void run() {
                    try {
                        ProDialog = ProgressDialog.show(Home.this,
                                "Sending Data",
                                "Please wait while sending data...");
                        Looper.prepare();
                        sendLocation();
                        handler.sendEmptyMessage(0);
                        quit();
                        Looper.loop();
                    } catch (Exception e) {
                        ProDialog.dismiss();
                    }
                }

                public void quit() {
                    ProDialog.dismiss();
                    Looper.myLooper().quit();
                }
            }.start();
        }
    };
try {
    t.schedule(timertask, d.getDate(), 1000);
} catch (Exception e) {
        e.printStackTrace();
}

2 个答案:

答案 0 :(得分:6)

您需要初始化

  

第一

更改

try {
        t.schedule(timertask, d.getDate(), 1000);
    } catch (Exception e) {
        e.printStackTrace();
    }

try 
 {
    Timer t=new Timer();
    t.schedule(timertask, d.getDate(), 1000);
 } 
catch (Exception e) 
 {
   e.printStackTrace();
 }

答案 1 :(得分:1)

基本上NullPointerException会抛出所需对象为null的位置。

NullPointerException

的原因
  • 调用null对象的实例方法。
  • 访问或修改空对象的字段。
  • 将null的长度视为数组。
  • 访问或修改null的插槽,就像它是一个数组一样。
  • 抛出null,就好像它是一个Throwable值。
  • 应用程序应抛出此类的实例以指示其他非法用途 null对象。

在此链接中更加精心地解释What is a NullPointerException, and how do I fix it?

相关问题