Hi
I have created my Alarm, at compile time I don't have any problem, but at run time I receive the follo wing messages:
Exception in thread "main" java.lang.NullPointerException
at java.util.Timer.sched(Unknown Source)
at java.util.Timer.scheduleAtFixedRate(Unknown Source)
at Alarm.AlarmExecute(Alarm.java:20)
at Test.main(Test.java:11)
(With <- I have pointed out the line 11 of test.java and line 20 of alarm.java)
Thank you in advance.
[PHP]import java.util.Timer ;
public class Alarm {
private int s;
private int h;
private int m;
private int []timeNow;
private Timer timer;
private AlarmTask almTsk;
public Alarm (int hour, int min) { //hour , min ->alarm time
h=hour;
m=min;
s=30;
timer=new Timer(true);
}
public void AlarmExecute(){
try{
timer.scheduleAtFixedRate(almTsk, 0, s*1000); //sample at 30s <-
timeNow=almTsk.TimeNow();
if(timeNow[0]==h && timeNow[1]==m){
System.out.print("Alarm");
}
}
catch (IllegalArgumentException e1){
System.out.println("delay is negative, or delay ");
}
catch (IllegalStateException e2){
System.out.println("task was already scheduled or cancelled, timer was cancelled");
}
}
}
mport java.util.Calendar;
import java.util.TimerTask;
public class AlarmTask extends TimerTask{
private int[] timeNow;
private Calendar cal;
public AlarmTask(){
}
public void run(){ //sample of time
timeNow[0]=getHour();
timeNow[1]=getMinute();
}
public int getHour(){
return cal.get(Calendar.HOUR_OF_DAY);
}
public int getMinute(){
return cal.get(Calendar.MINUTE);
}
public int[]TimeNow(){
return timeNow;
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Alarm a;
a=new Alarm(22,04);
a.AlarmExecute(); <-
}
[/PHP]
}