5

In the following code
t.schedule(timertask, d.getDate(), 1000); is throwing NullPointer exception help me out

Goal :
My basic goal is to Run a method(every time after a fixed interval ) that will send some data to webservice from my android device

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 Answers2

5

You need to initialize your

t

first.

Change

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

To

try 
 {
    Timer t=new Timer();
    t.schedule(timertask, d.getDate(), 1000);
 } 
catch (Exception e) 
 {
   e.printStackTrace();
 }
Vipul
  • 27,808
  • 7
  • 60
  • 75
1

Basically NullPointerException throws where the required object is null.

The reasons for NullPointerException

  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.
  • Applications should throw instances of this class to indicate other illegal uses of the null object.

Explained in this link more eloborately What is a NullPointerException, and how do I fix it?

Community
  • 1
  • 1
Akilan
  • 1,707
  • 1
  • 16
  • 30