Throwing a null pointer Exception in Timer.Schedule ();

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

Goal:
My main goal is to execute 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(); } 
+6
source share
2 answers

You need to initialize

t

first.

Edit

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

For

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

Basically, a NullPointerException throws the desired null object.

Causes of NullPointerException

  • Call the instance method of the null object.
  • Access or change the field of a null object.
  • Taking null length as if it were an array.
  • Access or change slots with a value of zero, as if it were an array.
  • Throw zero as if it were a Throwable value.
  • Applications should throw instances of this class to indicate another illegal use of a null object.

This link is explained in more detail. What is a NullPointerException and how to fix it?

+1
source

Source: https://habr.com/ru/post/918155/


All Articles