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.


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

Results 1 to 2 of 2

Thread: Need Help Getting A Simple Stop-Watch Working?

  1. #1
    Junior Member
    Join Date
    May 2011
    Thanked 0 Times in 0 Posts

    Unhappy Need Help Getting A Simple Stop-Watch Working?

    So I am experimenting with a little stop-watch object and I can't figure out why it isn't working. I have a gui with the following;
    timeinfield is a jtextfield which the user enters the start time of the clock.
    timerfield is a jtextfield where the clock is displayed timing.
    startbutton is a jbutton where the user clicks to start the clock.
    stopbutton is a jbutton where the user clicks to stop the clock.

    I want the clock to look like 00.00.00 and keep updating until the user clicks stop. Then I am going to do something with the stop value later on.

    Right now, the timerfield just displays Running: 0 seconds.
    Nothing else happens, maybe I am doing this wrong for it is my first time trying something like this. I am about to start learning how to use threads so I thought this might help get me started.

    Could someone help me write something like this that corresponds to the buttons and areas I have above? This is what I have so far;
        private void startbuttonActionPerformed(java.awt.event.ActionEvent evt) {
                String starttim = timeinfield.getText();
                long startTime = Long.valueOf(starttim);
                boolean running = false;
                Timer timer = null;
                long time = (System.currentTimeMillis() - startTime)/1000;
                timerfield.setText("Running:" + time + "seconds");
                running = true;
                timerfield.setText("Running: 0 seconds");
                if(stopbutton.isSelected()) {
                running = false;
                long endTime = evt.getWhen();
                double seconds = (endTime - startTime)/1000.0;
                timerfield.setText("Time: " + seconds + "sec.");
    // going to need timer.stop() while timer is null or null pointer exception occurr

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 632 Times in 540 Posts

    Default Re: Need Help Getting A Simple Stop-Watch Working?

    You declare timer but initialize it to null. What do you expect to happen? This is going to cause a NullPointerException every time.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 4
    Last Post: August 10th, 2011, 11:16 AM
  2. logic error somewhere.. working with try & catch, simple array.. please help
    By basketball8533 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 9th, 2010, 11:40 AM
  3. can' stop working a part of code.plzz help
    By kyros in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 03:10 PM
  4. Code Stop working after converting to jar?
    By Ron6566 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 16th, 2010, 12:17 PM
  5. radio buttons stop working
    By tabutcher in forum AWT / Java Swing
    Replies: 2
    Last Post: March 5th, 2010, 08:28 AM