Reputation: 131
So I'm doing this awkward example to understand how threading in Java works. It's quite simple actually, however, I can't seem to get why this gives me a NullPointerException exception when it tries to fire more than 3 threads.
Can you sort it out? Eclipse's debugger doesn't help :-(
Thanks in advance!!
public class Main {
public static void main(String[] args) {
Main boot = new Main();
}
public Main()
{
CoolThread myThread = new CoolThread(1, 2);
Thread t_myThread = new Thread(myThread);
t_myThread.start();
myThread.defineMain(this);
}
public void teste(String tests){
CoolThread myThread = new CoolThread(1, 2);
Thread t_myThread = new Thread(myThread);
t_myThread.start();
}
}
public class CoolThread extends Thread {
int firstNum;
int secondNum;
Main myMain;
/**
* Constructor
* @param firstNum
* @param secondNum
*/
public CoolThread(int firstNum, int secondNum)
{
this.firstNum = firstNum;
this.secondNum = secondNum;
}
public void defineMain(Main myMain)
{
this.myMain = myMain;
}
/**
* Fires the thread.
*/
public void run()
{
try{
int number = 0;
for(;;)
{
int soma = (firstNum+secondNum);
System.out.println("ID: " +Thread.currentThread().getId());
firstNum++;
secondNum++;
number++;
Thread.sleep(100);
if((number % 10) == 0)
{
myMain.teste("The sum is: " + soma);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
By the way, this is the output I get:
ID: 9
ID: 9
ID: 9
ID: 9
ID: 9
ID: 9
ID: 9
ID: 9
ID: 9
ID: 9
ID: 9
ID: 12
ID: 9
ID: 12
ID: 12
ID: 9
ID: 12
ID: 9
ID: 12
ID: 9
ID: 12
ID: 9
ID: 12
ID: 9
ID: 12
ID: 9
ID: 12
ID: 9
ID: 9
ID: 12
ID: 9
ID: 14
java.lang.NullPointerException
at my.own.package.CoolThread.run(CoolThread.java:44)
at java.lang.Thread.run(Thread.java:722)
And it goes on creating and killing threads...
Upvotes: 1
Views: 11497
Reputation: 116878
You are either calling myThread.defineMain(...)
after starting your thread (in Main
) or not calling defineMain(...)
at all (in teste(...)
). You need to define main before the thread is running otherwise there is a chance that myMain
is null
when you get to line 44 which I assume is:
myMain.teste("The sum is: " + soma);
This is the definition of a thread race condition. Your start code should be:
CoolThread myThread = new CoolThread(1, 2);
// this must be done _before_ the thread starts below
myThread.defineMain(this);
Thread t_myThread = new Thread(myThread);
t_myThread.start();
Never think that the JDK is wrong. That will just kill any debugging and critical thinking that you are using to find your problem. Learn how to use the debugger in eclipse. You can then put a break-point on line 44 and investigate the variables.
Unfortunately in this case you have a threaded program and the debugging is changing the timing of the program and most likely hiding the bug. You might have tried to print out the various objects on line 44 to see which one was null
.
Also, as @kurtzbot pointed out, if CoolThread
extends Thread
then you can just say new CoolThread()
and then coolThread.start()
. Really what you should be doing is having CoolThread
implement Runnable
instead of extending Thread
. That's the better pattern:
CoolThread myThread = new CoolThread(1, 2);
// this must be done _before_ the thread starts below
myThread.defineMain(this);
Thread t_myThread = new Thread(myThread);
t_myThread.start();
...
public class CoolThread implements Runnable {
public void run() {
...
}
}
Upvotes: 8
Reputation: 7894
I suspect myMain is null when a new thread is started from the teste method. You need to call defineMain from within the teste method like you do in the constructor for Main class.
Upvotes: 0