|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
What is wrong with this code?
Hi friends, I have a problem with the following code. Can anyone help me with it?
When I compile to execute the ReminderBeep.java, I ate the exception listed below. When I debug the program the t2 reference is going to be null, after new ReminderBeep(5); statement. What is the problem? Regards... Task is scheduled java.lang.NullPointerException at misc.ReminderBeep$RemindTask.run(ReminderBeep.java :32) at java.util.TimerThread.mainLoop(Unknown Source) at java.util.TimerThread.run(Unknown Source) /* ReminderBeep.java * Created on 19.May.2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package misc; import java.awt.Toolkit; import java.util.Timer; import java.util.TimerTask; /** * @author korayy * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class ReminderBeep { Toolkit toolkit; Timer timer; Timer2 t2; public ReminderBeep(int seconds){ toolkit = Toolkit.getDefaultToolkit(); timer = new Timer(); timer.schedule(new RemindTask(), seconds*1000); } class RemindTask extends TimerTask{ public void run(){ System.out.println(t2.elapsedTime()); System.out.println("Time's up!"); toolkit.beep(); //timer.cancel(); //Not necessary because we call System.exit() System.exit(0); //Stops the AWT thread (and everything else) } } public static void main(String[] args) { Timer2 t2 = new Timer2(); t2.resetTimer(); new ReminderBeep(5); System.out.println("Task is scheduled"); } } ----------------------------------------------- /* Timer2.java * Created on 17.May.2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package misc; /** * @author korayy * * * Purpose: Object to measure the elapsed time between the most * recent call to method resetTimer() and the call to method elapsedTime(). * This class creates and starts a timer when a Timer object is instantiated, and * elapsedTime() is calculated. */ public class Timer2 { //Define the instance variables private double savedTime; //Define the class costructor public Timer2(){ resetTimer(); } //Define the methods public void resetTimer(){ savedTime = System.currentTimeMillis(); } public double getSavedTime(){ return savedTime; } /** Finds the elsapsed time in ms * * @param * @return the elapsed Time from the instance is instantiated */ public double elapsedTime(){ double eTime; eTime =(System.currentTimeMillis() - getSavedTime()) ; return eTime; } public String toString(){ return "Elapsed time is = " + elapsedTime() + "ms" ; } } |
|
#2
|
||||
|
||||
|
You declared a Timer2 t2 as an attribute for the ReminderBeep class but in the main method you also declare a Timer2 t2. You initialise the last t2 but that one is not in the scope of the RemindTask class. The t2 in the RemindBeep class never gets initialised and that's why you get a nullpointer exception.
You should move these two lines into the constructor: Timer2 t2 = new Timer2(); t2.resetTimer(); Furthermore you should change Timer2 t2 = new Timer2(); into t2 = new Timer2(); This way you will initialise the class member instead of the local variable in main. Hope this helps |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > What is wrong with this code? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|