468,119 Members | 1,878 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,119 developers. It's quick & easy.

Can't stop the Timer....!

Hey.. I'm having problems with stopping this timer from outside the
'runTimer()' class.. i have no problems calling 'cancel()' from inside
the run() method but I want to be able to stop the Timer when I press
a button (stopTimer()).

With the code I've got here, i get an error saying 'void cannot be
dereferenced' ... has anyone got any ideas as to what I'm doing wrong
or how i could stop the timer by pressing the button? Any help would
be appreciated.. i'm fairly new to java so if you can keep it in
relative laymens terms thanks! :)

public void runTimer(){

int delay = 0; //
int period = 3000; //

java.util.Timer timer = new java.util.Timer();

timer.scheduleAtFixedRate(new TimerTask() {

public void run() {
//code does stuff here
}
}, delay, period);
}
//when stopTimer button is pressed.. timer should stop
public void stopTimer() {
runTimer().timer.cancel();
}
Jul 17 '05 #1
1 10680
ge******@hotmail.com (Geoff) wrote in news:18143a5b.0403310531.7b0efb72
@posting.google.com:
Hey.. I'm having problems with stopping this timer from outside the
'runTimer()' class.. i have no problems calling 'cancel()' from inside
the run() method but I want to be able to stop the Timer when I press
a button (stopTimer()).

With the code I've got here, i get an error saying 'void cannot be
dereferenced' ... has anyone got any ideas as to what I'm doing wrong
or how i could stop the timer by pressing the button? Any help would
be appreciated.. i'm fairly new to java so if you can keep it in
relative laymens terms thanks! :)

public void runTimer(){

int delay = 0; //
int period = 3000; //

java.util.Timer timer = new java.util.Timer();

timer.scheduleAtFixedRate(new TimerTask() {

public void run() {
//code does stuff here
}
}, delay, period);
}
//when stopTimer button is pressed.. timer should stop
public void stopTimer() {
runTimer().timer.cancel();
}


Why is your method stopTimer() referencing runTimer() ??
runTimer() returns void, just as the compiler told you, so how could it
have a method or field called timer ?

I think that you meant timer.cancel(), NOT runTimer().timer.cancel()

Good Luck!
--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Trina Schwimmer | last post: by
3 posts views Thread by Ian Richardson | last post: by
2 posts views Thread by Elliot | last post: by
15 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.