LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-20-2006, 06:01 AM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 424

Rep: Reputation: 30
java timers :@


hello, im trying to run this function on one of my java forms every second that the program is running, so i placed a timer in a thread, however i keep getting a null pointer exception?!

code:
Code:
        public void checkEvents() {
                ( new Thread() {
                    public void run() {
                        
                        while( true ) {
                            if( runClock == true ) {
                                timer.schedule( new task(), 1000 );
                            }
                        }
                    }
                }).start();
        }

        class task extends TimerTask {
            public void run() {
                print( "hello there" );
                runClock = true;
                timer.cancel();
            }
        };
cheers, trscookie.
 
Old 07-20-2006, 06:38 AM   #2
mrcheeks
Senior Member
 
Registered: Mar 2004
Location: far enough
Distribution: OS X 10.6.7
Posts: 1,690

Rep: Reputation: 50
Where do you get a null pointer exception at what line? Is it the task which is null or something else?
 
Old 07-20-2006, 07:04 AM   #3
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 424

Original Poster
Rep: Reputation: 30
at this line:

Code:
timer.schedule( new task(), 1000 );
ive also tried:

Code:
timer.schedule( new task(), 0, 1000 );
ad get the same problem
 
Old 07-20-2006, 10:02 AM   #4
mrcheeks
Senior Member
 
Registered: Mar 2004
Location: far enough
Distribution: OS X 10.6.7
Posts: 1,690

Rep: Reputation: 50
I think you forgot to initialize the timer.
Code:
timer = new Timer();
 
Old 07-20-2006, 10:42 AM   #5
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 424

Original Poster
Rep: Reputation: 30
oooop yea just changed that cheers, the other problem is that it doesnt seem to wait the time before printing "hello there" it just print millions of them. how can i get it to print one then wait then print the next?? cheers trscookie
 
Old 07-20-2006, 10:50 AM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
i think youre looking for sleep..
 
Old 07-20-2006, 10:50 AM   #7
mrcheeks
Senior Member
 
Registered: Mar 2004
Location: far enough
Distribution: OS X 10.6.7
Posts: 1,690

Rep: Reputation: 50
your print method is in the method run of your task. It will print hello until the timer is canceled. You are asking the timer to wait for one second...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to build kernel timers? santhosh.linux Programming 1 12-14-2005 09:10 PM
Threads & Timers Reefath Linux - Newbie 1 04-01-2005 07:44 PM
about timers eshwar_ind Programming 1 12-27-2004 09:23 AM
C - Linux - Timers - How? drigz Programming 6 08-30-2004 02:36 PM
Help with Linux timers samble Programming 8 02-07-2004 10:54 PM


All times are GMT -5. The time now is 04:44 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration