There is a runnable method called r1
Runnable r1=new Runnable(){
@Override
public void run(){
//some code here
}
};
I created a thread th1
Thread th1;
Now, in window Activated of my main frame I write this:
private void formWindowActivated(java.awt.event.WindowEvent evt) {
setLocationRelativeTo(null);
Thread th1=new Thread(r1); //For those who want to know I initialized the thread twice that was the problem damn!
th1.start();
}
Then in the unlikely event that the user quickly passes on to a child frame named f1
while the thread th1
is still running , I want it to show a loading screen , nothing fancy just a simple gif for now. This loading frame is named "loading".
So, I wrote this:
private void f1formWindowActivated(java.awt.event.WindowEvent evt) {
f1.setLocationRelativeTo(null);
while(th1.isAlive())
{
loading.setVisible(true);
}
loading.dispose();
}
and this is the problem I get a null pointer exception
and the problem is in this line:
th1.isAlive()
If I comment This everything works fine.
PS:
I have removed a lot of the code from my original code to save you the hassle I am fairly certain the rest of the code works fine. Also, I code in netbeans. And, if it matters I have another thread which is called th
and but the thread does not start until you click a jButton
which is present in f1
hope this information is sufficient. Help. Thanks
Edit:
Thread th1
is global
th1
always gets initialized first infact the thread even starts running it keeps on running until window activated event of f1
.