473,414 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

java.util.Timer.sched(Unknown Source)

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
4 5407
horace1
1,510 Expert 1GB
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
JosAH
11,448 Expert 8TB
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
Thank you very much.
I apologize for posting a silly error.
Apr 1 '07 #4
horace1
1,510 Expert 1GB
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

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

Similar topics

0
by: news.dsl.pipex.com | last post by:
Greetings, I'm trying to find out how in the source you set the version/vendor/title information so that it can be read at runtime. All I've found is mention of ClassLoader in relation to...
9
by: FISH | last post by:
Ever have one of those days when you're not sure if it's you who's gone mad, or the rest of the world? I have an Open Source project on SourceForge for communication with YSMG - Yahoo's IM...
2
by: Edward Berard | last post by:
I am looking for tools that will analyze Java source code: -> I am primarily looking for metrics tools, e.g., tools that will tell me things like - Total lines of code - Total blank lines...
0
by: Petterson Mikael | last post by:
Hi, I am using Solaris 8 and JDK1.4.1 I will generate java source files from XML. I am thinking of using JAXB package. As input I have my XML and DTD. I setup "Java Web Services Developer Pack...
1
by: HeroinNO.4 | last post by:
You can open http://www.fillweb.com in IE and View->Source to see the latest version full featured count down timer source code, or you may also copy the code below and save in a "*.htm" file, for...
2
by: HeroinNO.4 | last post by:
Hello everyone! Now the latest version of free count down timer source code is available in http://www.fillweb.com/countdown.htm, you can open it in IE and View->Source to see the latest version...
7
by: HeroinNO.4 | last post by:
Hello guys, free count down timer source code has updated to 06/11/27, you can copy the code below and save in a ".htm" file and run it in a browser support javascript 1.1 or later, or you can open...
2
Saghar
by: Saghar | last post by:
Hi, Could somebody help me about this topic? I want to know how we can call and execute a non-java source file in a java application. For example, a simple Java application is run, there is an...
1
by: cwj | last post by:
Can anybody tell me some tools that can transfer java source code to cpp source code?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.