0

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.

Zain Arshad
  • 1,885
  • 1
  • 11
  • 26
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – xtratic Apr 02 '19 at 15:07
  • in the method `formWindowActivated` you create a local variable `th1`. That is not the same as the variable (or more likely the field which you probably declare somewhere) you access with the `isAlive()` line. That means your field remains `null`. – f1sh Apr 02 '19 at 15:08
  • th1 is global. So, what should I do so as to prevent the error – Yash Yelmame Apr 02 '19 at 15:22
  • 3
    you have 2 things called `th1`. One is "global" (a field in your class), one is a local variable, declared in the line `Thread th1=new Thread(r1);`. Change that to `th1=new Thread(r1);` and you solved that. – f1sh Apr 02 '19 at 15:24
  • Oh got it damn I initialized it twice thanks man thats just stupid wow – Yash Yelmame Apr 02 '19 at 15:26

2 Answers2

3

Thread th1 =... creates a new local variable which hides the global variable.

Other possible causes: Your code only works if the user has to activate formWindow first. If that's not the case, then the thread is never created.

A better solution would be to create the form f1 with the loading screen visible and disable that at the end of the thread.

If this is not the case, you either have two variables th1 (and you initialize only one of them) or somewhere in the code th1 is set to null.

Use your debugger to see the values of the variable and when they change.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • formWindow is always activated before f1 . It is a login page. – Yash Yelmame Apr 02 '19 at 15:23
  • I want to downvote this because the problem you are describing is not the cause. If you look at the code of the OP, you can see he declared the variable th1 2 times, so it doesn't matter how often you activate the first form, the second declaration would always be empty – Ferrybig Apr 02 '19 at 15:54
  • @Ferrybig I edited my answer and wrote "you either have two variables". – Aaron Digulla Apr 02 '19 at 15:59
1

The Problem for those who must know was that I had Initialized the thread twice once globaly and once in windowActivated credits to @f1sh Damn I don't know how to give credit so i just typed an '@'