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++;
}
}