Hello i am having a problem with resuming a thread my code is
public boolean Wait(String Reply){
if (Reply.equalsIgnoreCase("Y")){
try {
t.resume();
}
catch (Exception e){
System.out.println("\n" + "The exception in resume thread method:::: " + e);
}
System.out.println("\n" + "In the Wait Function of Sender");
return true;
}
JOptionPane.showMessageDialog(j ,
"Please Wait While The User Accpets the Trasmission ",
"",
JOptionPane.INFORMATION_MESSAGE);
try{
t = new Thread(this);
t.sleep(100000);
}
catch (InterruptedException ie){
System.out.println(ie.getMessage());
}
return false;
}
I might explain you how it works as it will help u determine the problem.
First the thread is put to sleep……Then i call this public boolean Wait() function from another function named ReplyYes which passes the value “Y” and i then try to resume the thread but the t.resume() function call, instead of resuming the thread gives me a Java.Lang.Null.PointerException and the thread isn’t resumed resulting in returning a FALSE value. Plus because of this thread i can’t even Stop my Service i have to wait for the thread to timeOut.
Can anyone explain how to make it work correctly!!
Thank you
I think you misunderstand how
Thread.sleepworks. It is a static method.The line
t.sleep(100000);puts the current thread to sleep, not the threadt.From the documentation:
Emphasis mine.
You should start the thread and call sleep from that thread. See the following article for two different ways to start a thread:
Furthermore,
resumeis only for use withsuspendand they have both been deprecated. From the documentation:The reason you get a
NullPointerExceptionis probably because you try to create the new Thread object after you callt.resume(). So at that point, t still has the valuenull. Basically, your code needs to be completely rewritten from scratch. I would suggest following the tutorial I linked to above, then once you understand how to create threads move to the next chapters: