This is an attempt to stop the timer in this code: Click Here

I just learned threads but I'm not sure if I'm using it right..
It doesn't have any erros when I compile it.. but it still doesn't stop the time.
the clock starts when you enter "y" though..
How can I use threads to stop the timer?

COMMAND PROMPT OUTPUT:
Enter "n" to Stop.
    Exception in thread "Thread-1" java.lang.NullPointerException
  at myClockTest$2.run(myClockTest.java:42)
    Set Seconds:1

    Set Minutes:2

    Set Hours:3

    Set Days:4
    Enter "y" to start clock.
    **
myClockTest
import java.io.*;
import java.util.*;
    public class myClockTest
{       static  String stopIt;
        public static void main (String[]args)
        {
                Thread t = new Thread()
                     {//start t
                        public void run(){
                            Scanner setset = new Scanner(System.in);
                              myClock mc = new myClock (00 ,00, 00, 00);

                                    System.out.print( "Set Seconds:");
                                    int secondsSet = setset.nextInt();
                                    System.out.println();
                                    System.out.print( "Set Minutes:");
                                    int minutesSet = setset.nextInt();
                                    System.out.println();
                                    System.out.print( "Set Hours:");
                                    int hoursSet = setset.nextInt();
                                    System.out.println();
                                    System.out.print( "Set Days:");
                                    int daysSet = setset.nextInt();



                                          mc.AskstartStop();
                                          mc.setTime( daysSet,hoursSet, minutesSet, secondsSet);
                                          mc.incrementSecond();
                                          System.out.println();
                                              mc.printTime();


                            }};//end of t

                t.start();

            Thread t2= new Thread()
                            {// start t2
                             public void run(){
                            System.out.println (" Enter \"n\" to Stop.");
                             if (stopIt.equalsIgnoreCase("n") == true )
                             {{System.exit(0);} }
            }
            }; // end t2

            t2.start();





        }




}
myClock CLASS
import java.io.*;
import java.util.*;
public class myClock
{
    static Scanner scan = new Scanner (System.in);
    private int hr;
    private int min;
    private int sec;
    private int da;
    private String startStop;

            public myClock()
            {
                setTime(0 , 0, 0 , 0);
            }


            public myClock(int d, int h, int m , int s)
            {
                setTime(d, h , m , s);
            }
             public void setTime(int day , int hour , int minute , int second )
             {
                if (0 <= day )
                    da = day;
                else
                    da = 0;
                if (0 <= hour && hour < 24 )
                    hr = hour;
                else
                    hr = 0;
                if (0 <= minute && minute < 60)
                    min = minute ;
                else
                    min = 0;
                if (0 <= second && second < 60 )
                    sec = second;
                else sec = 0;
             }

            public int getDay()
                {return da;}
             public int getHour()
                { return hr;}
            public int setMin()
                {return min; }
            public int getSec()
                {return sec;}
            public void printTime()
            {
                    if(da <= 10)
                        System.out.print("0");
                        System.out.print(da + ":");
                    if(hr < 10)
                        System.out.print("0");
                        System.out.print(hr + ":");
                    if (min <10)
                        System.out.print("0");
                        System.out.print (min + ":");
                    if (sec < 10)
                        System.out.print("0");
                        System.out.print(sec + ":");
            }

                public void AskstartStop()
                {
                    System.out.println ("Enter \"y\" to start clock.");
                    startStop = scan.next();
                }

            public void incrementSecond()
            {
                while (startStop.equalsIgnoreCase("y"))
                {
                    sec++;
                    System.out.println();
                    printTime();
                    if (sec > 59)
                    {
                    sec = 0 ;
                    incrementMinute();
                    }

                        //if (startStop.equalsIgnoreCase("n") == true )

                            //{System.exit(0);}
                }
            }
            public void incrementMinute()
            {
                min++;
                if ( min > 59)
                {min = 0;
                incrementHour();
                }
            }
            public void incrementHour()
            {
                hr++;
                if (hr > 23)
                    {hr = 0;
                incrementDay();}
            }
            public void incrementDay()
            {
                    da++;
                }
}

Try debugging the code by adding lots of println statements to the code that show the values of variables as they are used and as they are changed and to show the program execution flow so you can see what the computer sees and understand what your program is doing.

You're heading in a right direction, but there's something missing between line 41 (where you prompt the user) and line 42 where you test the user's input (that you haven't read yet!).

commented: oh wait.. now it asks for 2 inputs in set Seconds. and I still can't enter "n" to stop it.. +0

stopIt

Is defined as a String variable, but where is it set?

commented: yeah. i forgot to scan the input for stopIt. I fixed it now.. but it still doesn't work. +0
UPDATE

I forgot to scan for an input for stopIt. I fixed it now.. but there's still something wrong with the output.
LOOK.. it asks for Set Seconds: twice..
when Enter "n" on the second one.. it stops the program..
That's the only time I get to enter "n"

OUTPUT
 Enter "n" to Stop.
Set Seconds:2
2

Set Minutes:2

Set Hours:2

Set Days:2
Enter "y" to start clock.
myClockTest
import java.io.*;
import java.util.*;
    public class myClockTest
{
    static Scanner stopItscanner = new Scanner(System.in);
    static  String stopIt;
        public static void main (String[]args)
        {








                Thread t = new Thread()
                     {//start t
                        public void run(){
                            Scanner setset = new Scanner(System.in);
                              myClock mc = new myClock (00 ,00, 00, 00);

                                    System.out.print( "Set Seconds:");
                                    int secondsSet = setset.nextInt();
                                    System.out.println();
                                    System.out.print( "Set Minutes:");
                                    int minutesSet = setset.nextInt();
                                    System.out.println();
                                    System.out.print( "Set Hours:");
                                    int hoursSet = setset.nextInt();
                                    System.out.println();
                                    System.out.print( "Set Days:");
                                    int daysSet = setset.nextInt();



                                          mc.AskstartStop();
                                          mc.setTime( daysSet,hoursSet, minutesSet, secondsSet);
                                          mc.incrementSecond();
                                          System.out.println();
                                              mc.printTime();



                            }};//end of t

                t.start();


                        Thread t2= new Thread()
                            {// start t2
                                 public void run()
                                 {

                                        System.out.println (" Enter \"n\" to Stop.");
                                        stopIt = stopItscanner.next();
                                         if (stopIt.equalsIgnoreCase("n") == true )
                                        {{System.exit(0);} }
                                        }

                            }; // end t2

                        t2.start();

it asks for Set Seconds: twice..

Your posted output does not show that. Can you post the full contents of the console that shows where it asks twice?

commented: Enter "n" to Stop. Set Seconds:2 After entering 2 and pressing enter it asks for input again then I entered 2 again. and that's why there are two "2"s in the output. +0

If you notice.. there are two 2's after Set Seconds...
After entering the first "2"..
It expected me to enter another input
in the next line.. So I entered another "2" which
made it ask for set Minutes, hours, etc...

I also tried to enter "n" on the 2nd one..
and it closed the program.. so I guess the 2nd one is
from is for

 stopIt = stopItscanner.next(); 

and the first one from

int secondsSet = setset.nextInt();

Change the empty println() statements on lines 25, 28 & 31 and the other empty println()s to print out the values that were read so you can see where the code is executing and what was read in. Be sure to add an id String to identify the name of the variable that is being printed: println("var="+var +"<")

Having two threads competing for input from the same system in device is asking for trouble. Much better to have all the user I/O in one single thread, and have the timer running in its own (no UI) thread.

Can someone just tell me how to code it exactly??
I still have another homework to do X-X

I removed the set time stuff since I don't really need them...
NOW IT'S LIKE THIS. The time keeps moving and I never get to input "n".

import java.io.*;
import java.util.*;
    public class myClockTest
{
    static Scanner stopItscanner = new Scanner(System.in);
    static  String stopIt;
        public static void main (String[]args)
        {

                                    myClock mc = new myClock (00 ,00, 00, 00);
                                    mc.AskstartStop();
                                    mc.incrementSecond();

                    Thread t = new Thread()
                    {//start t
                        public void run(){

                             try
                                {Thread.sleep(100);

                                    myClock mc = new myClock (00 ,00, 00, 00);

                                    System.out.println();
                                    mc.printTime();
                                }catch(Exception e){}


                            }};//end of t

                new Thread(t).start();


                        Thread t2= new Thread()
                            {// start t2
                                 public void run()
                                 {

                                         try
                                         {                  Thread.sleep(1000);
                                                         System.out.println (" Enter \"n\" to Stop.");
                                                            stopIt = stopItscanner.next();
                                                            if (stopIt.equalsIgnoreCase("n") == true )
                                                            {{System.exit(0);} }

                                        }catch(Exception e){}
                                    }

                            }; // end t2

                new Thread (t2).start();






        }




}


                            /*Scanner setset = new Scanner(System.in);
                              myClock mc = new myClock (00 ,00, 00, 00);

                                    System.out.print( "Set Seconds:");
                                    int secondsSet = setset.nextInt();
                                    System.out.println();
                                    System.out.print( "Set Minutes:");
                                    int minutesSet = setset.nextInt();
                                    System.out.println();
                                    System.out.print( "Set Hours:");
                                    int hoursSet = setset.nextInt();
                                    System.out.println();
                                    System.out.print( "Set Days:");
                                    int daysSet = setset.nextInt();
                                     mc.setTime( daysSet,hoursSet, minutesSet, secondsSet);*/

Can someone just tell me how to code it exactly??
I still have another homework to do ;u;

Sorry, that's not going to happen,this isn't a "we do your homework" service. Keep trying, and someone will keep helping.

You need to understand what the code is doing and how it is executing so you can fix it. One way to do that is to add lots of println statements that print out messages showing the execution flow and the values of variables as the code is executed. The print out will help you see what the code is doing.

I don't understand why I should print the variables??
What should I print?

I already know what I inputed for...
stopIt and AskstartStop();

like if I entered "n" .... I know that if I print System.out.println (stopIt);
will just print n.. o3o

and the timer is pretty much working fine...

I'm so confused now. Threads aren't even in our lesson yet! o_O

you need to see what the code is doing so you can change it to do what your want it to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.