-1

I'm trying to write a timer that will count down from 10 and then stop when it gets to 0 but in the 'timer.cancel();' part at the bottom of the code, there is a red line underneath 'timer'. It says that I haven't defined 'timer'. I did define 'timer' earlier on in the code so I'm not sure what I'm doing wrong?

(I am doing this in NetBeans 8.2)

package javaapplication3;
import java.util.Timer;
import java.util.TimerTask;

public class JavaApplication3 {
     public static void main(String[] args) {
        Timer timer = new Timer();
        timer.schedule(new timeInterval(), 0, 1000);
    }
}

class timeInterval extends TimerTask {

    int countdown = 10;

    public void run() {
        countdown = countdown - 1;
        System.out.println(countdown);
        if (countdown <= 0) {
            timer.cancel();
    }

}
}
ejaf12
  • 1
  • 2
  • 3
    Possible duplicate of [What Is Scope In Java](https://stackoverflow.com/questions/38177140/what-is-scope-in-java) – Joe C Dec 27 '18 at 19:41
  • Please note that simply killing a timer task will not kill the timer that scheduled it – Dan Dec 27 '18 at 20:00

2 Answers2

0

This is a scope problem. You can't refer to the named object timer in the class TimerTask because it has no knowledge of it.

Possibly check out this article on scope of variables

you can try just calling the cancel() function. You can read about the TimerTask java class here

if (countdown <= 0) {
  cancel();
}

You should also get in the habit of making your class variables private, unless you have a good reason not to.

Note this will not kill the Timer object, only the task scheduled for it. If you don't plan on using it again, you should add some code in to cancel it.

Matt Goodis
  • 393
  • 1
  • 3
  • 11
  • 1
    Hey Matt, simple calling cancel will not kill the timer and the application will remain running as the timer is running. Even if the timer task does not run, the timer is still there – Dan Dec 27 '18 at 19:56
  • Agreed, but it will cancel the task associated with the Timer, which is what i read as the desired effect? – Matt Goodis Dec 27 '18 at 20:00
  • Fair enough. I suppose I just took the desired effect as different due to the scope error caused by `timer.cancel()` – Dan Dec 27 '18 at 20:08
  • 1
    Sorry I should've been more specific! But don't worry I've added in some code which stops the program :) – ejaf12 Dec 27 '18 at 20:39
0

the issue here is timer is out of scope. In other words, it cannot be seen outside of public static void main(String[] args). I would suggest you have a look at this webpage for more on Scope

A simple fix for this is to simply pass the timer in using a constructor. See below how you call TimerInterval(timer).

Note

I renamed your class so it met standard Java naming conventions. See Java Naming Conventions

import java.util.Timer;
import java.util.TimerTask;

public class JavaApplication3 {
     public static void main(String[] args) {
        Timer timer = new Timer();
        timer.schedule(new TimeInterval(timer), 0, 1000);
    }
}

class TimeInterval extends TimerTask {
    private Timer t;
    public TimeInterval(Timer t) {
        this.t = t;
    }

    int countdown = 10;

    public void run() {
        countdown = countdown - 1;
        System.out.println(countdown);
        if (countdown <= 0) {
            t.cancel();
        }
    }
}
Dan
  • 7,286
  • 6
  • 49
  • 114