Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Timer not running.

  1. #1
    Junior Member
    Join Date
    May 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Timer not running.

    Hello, this code is not running.

    package countdown;
     
    import java.util.Timer;
    import java.util.TimerTask;
     
    public class counter {
        public static TimerTask run() {
        	System.out.println("hello");
        	return null;
        }
        public static void main(String args[]) {
        	Timer t = new Timer();
            t.schedule(run(), 0, 1000);
        }
    }

    This is the console message.

    Exception in thread "main" java.lang.NullPointerException: Cannot read field "lock" because "task" is null
    at java.base/java.util.Timer.sched(Timer.java:400)
    at java.base/java.util.Timer.schedule(Timer.java:249)
    at countdown/countdown.counter.main(counter.java:14)
    Last edited by Bbax; May 16th, 2021 at 05:45 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Timer not running.

    The run() method returns null instead of an instance of the TimerTask class that the schedule method requires.
    Change run to return a valid instance of the TimerTask class.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Timer not running.

    I want the run function to be outside of main function.

    package countdown;
     
    import java.util.Timer;
    import java.util.TimerTask;
     
    public class counter {
        public static void main(String args[]) {
        	Timer tick = new Timer();
        	TimerTask myTask = new TimerTask() {
            @Override
            public void run() {
            	System.out.println("hello");
            }
            };
            tick.scheduleAtFixedRate(myTask, 0, 1000);
        }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Timer not running.

    You have the code, now move the declaration of an instance of the TimerTask class outside of the main method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. TIMER HELP PLEASE
    By javados in forum AWT / Java Swing
    Replies: 21
    Last Post: October 8th, 2014, 08:56 AM
  2. [SOLVED] Need some help in a timer
    By Kerooker in forum What's Wrong With My Code?
    Replies: 80
    Last Post: April 28th, 2014, 03:14 PM
  3. Replies: 21
    Last Post: June 12th, 2013, 11:33 AM
  4. How to put a timer?!?
    By mrprogrammer in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 21st, 2012, 10:44 AM
  5. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM