簡體   English   中英

在Timer.Schedule()中引發null指針異常;

[英]Throwing null pointerException in Timer.Schedule();

在下面的代碼中
t.schedule(timertask,d.getDate(),1000); 拋出NullPointer異常幫助我

目標:
我的基本目標是運行一個方法( 每次在固定間隔后 ),該方法將從我的android設備Web服務 發送一些數據

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();
}

您需要初始化

Ť

第一。

更改

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();
 }

基本上NullPointerException拋出要求的對象為null

NullPointerException的原因

  • 調用空對象的實例方法。
  • 訪問或修改空對象的字段。
  • 將null的長度視為數組。
  • 訪問或修改null插槽,就好像它是一個數組一樣。
  • 將null拋出,就好像它是一個Throwable值一樣。
  • 應用程序應拋出此類的實例,以指示對null對象的其他非法使用。

在此鏈接中更詳細地解釋什么是NullPointerException,我該如何解決?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM