Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 19th, 2005, 12:00 PM
KOrgi KOrgi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 1 KOrgi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 49 sec
Reputation Power: 0
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" ;
}
}

Reply With Quote
  #2  
Old September 29th, 2005, 05:50 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 734 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 11 h 57 m 2 sec
Reputation Power: 4
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
Comments on this post
MichaelSoft agrees: That sounds just right

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > What is wrong with this code?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT