By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,610 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,610 IT Pros & Developers. It's quick & easy.

java.util.Timer.sched(Unknown Source)

P: 4
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]
}
Mar 31 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 1,510
in
Expand|Select|Wrap|Line Numbers
  1. timer.scheduleAtFixedRate(almTsk, 0, s*1000); //sample at 30s <-
  2.  
the object almTsk is null
Apr 1 '07 #2

Expert 10K+
P: 11,448
You did create a Timer in your Alarm constructor but you forgot to create an
AlarmTask object, hence your almTsk variable is just implicitly initialized to null.

kind regards,

Jos
Apr 1 '07 #3

P: 4
Thank you very much.
I apologize for posting a silly error.
Apr 1 '07 #4

Expert 100+
P: 1,510
Thank you very much.
I apologize for posting a silly error.
no problem - errors like this can be difficult to find.
The thing to do when you get an error like this is to look at the runtime exception traceback which is printed on the screen, e.g.
Exception in thread "main" java.lang.NullPointerException
at java.util.Timer.sched(Timer.java:356)
at java.util.Timer.scheduleAtFixedRate(Timer.java:296 )
at Alarm.AlarmExecute(Test.java:42)
at Test.main(Test.java:148)

it tells you the problem is on line 42 of file Test.java which was called form line 148
If you cannot see what the problem is (by examining line 42) try printing the values of the objects, e.g.
System.out.println(" " + almTsk);

which will tell you which one was null
Apr 1 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.