This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Nullpointerexception when using a looping thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Nullpointerexception when using a looping thread" Watch "Nullpointerexception when using a looping thread" New topic
Author

Nullpointerexception when using a looping thread

Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28
I made a short tester program to illustrate this:
SubTest's update method will regernate a Nullpointerexception when running at this state because reference p is apperently null when runned. But how can reference p ever be null really? When a new object of SubTest is created reference p should directly be initialized a value (new Person) because its written in class definition?

I noted that this can be worked around by adding to check if p is null, and if so, skip updating this round. What am I missing to understand here? Is the update method called in SubTest before like the whole "creation" of the object is complete so for a short time p equals null instead a of a Person object?

A bigger project of mine is built up on this kind of thread-looping and I have to to put in if-statements everywhere to check if references really pointing at a object when they are used. This feels like a bad way doing it, how can I solve this in a more "corect way"?

Thanks in advance!

Same question discussed:
http://forum.codecall.net/java-help/34347-nullpointerexception-when-using-looping-thread.html#post282474
http://forums.oracle.com/forums/thread.jspa?threadID=2135853&stqc=true


Code first, then think.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Kalle, please read this: BeForthrightWhenCrossPostingToOtherSites
Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28
I always follow up all my threads and make sure the best answer/solution/thoughts is posted there so others can get the possible useful information that may come up. So I don't see how it hurts, but thank you for informing.

Edit: Oh yes I should say that this question was posted on other forums as well to let you know, sorry.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18541
    
  40


Classic race condition... When a subtest object is instantiated...

1. The super constructor (ThreadTest) is called.
2. All the instance variables iniitializers of the subtest are executed.
3. The rest of the subtest constructor is called (of which there is none).

The thing to notice here is that the super constructor is done before the instance variables are initialized.

So... the super constructor creates a new thread, and the race is on. What will happen first? Will the main thread finish running the super constructor, and then initialize the p variable? Or will the newly created thread get started and call the update method?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28
Henry Wong wrote:
Classic race condition... When a subtest object is instantiated...

1. The super constructor (ThreadTest) is called.
2. All the instance variables iniitializers of the subtest are executed.
3. The rest of the subtest constructor is called (of which there is none).

The thing to notice here is that the super constructor is done before the instance variables are initialized.

So... the super constructor creates a new thread, and the race is on. What will happen first? Will the main thread finish running the super constructor, and then initialize the p variable? Or will the newly created thread get started and call the update method?

Henry


Haha, I see! That explain why not the NullpointerException is casted at all runtimes like you could expect then. Sometimes the program crash, sometimes not. Forgot to mention this.

So have I done bad programming here and should do it in an other way, or is the best way to just add if-statements to check if the references not equals null as I mentioned?
Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28


I guess. But how can that be done? I could use Thread.sleep, but as I have no idea how long time it takes for step 2 and 3 to be completed, especially in a big project I guess, that's a pretty bad solution.


[HENRY: formatted code]
Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28
oxano on Codecall mentioned that due to Henry's good summary of how a object is instantiated its better to do the in constructor of SubTest instead of the superclass ThreadTest.

Then if you make sure you put that start command of the thread in the very end of SubTest's constructor, you can be sure that step 1, 2 and 3 are done before the thread starting going, that in this case use a reference that might cause a NullpointerException if not initialized yet.

After testing that it worked fine. Just to make clear what changes I made:


My previous entry mentioned a solution that probably would have worked in most cases, I think, but this seems way better and stable.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Unfortunately the rule is: Don't call a method which can be overridden from a constructor. If you do, then you risk the problem you had here, and it can happen even with no threading involved. Generally that means removing those method calls from the constructor and putting them in some new method which you would call after creating the object.
Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28
doublepost.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2154
    
    7
Kalle Zander wrote:I always follow up all my threads and make sure the best answer/solution/thoughts is posted there so others can get the possible useful information that may come up.


So why haven't you updated the other postings indicating an answer has been found?

Kalle Zander wrote:So I don't see how it hurts, but thank you for informing.

Of course you don't see how it hurts because you aren't the one who has wasted their time answering a question. An answer was given here and then half an hour later an answer was given in another forum. The other person wasted their time because you cross posted.



Kalle Zander wrote:Edit: Oh yes I should say that this question was posted on other forums as well to let you know, sorry.


What good does that statement do. You didn't include links to the other postings so we can't check the answer on the other posting to see if we have the same suggestion.

Learn to use the forums properly, you've been asked once and still can't follow the suggestions.
Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28
Rob Camick wrote:
Kalle Zander wrote:I always follow up all my threads and make sure the best answer/solution/thoughts is posted there so others can get the possible useful information that may come up.


So why haven't you updated the other postings indicating an answer has been found?


Working on it!
Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28
Rob Camick wrote:
Kalle Zander wrote:Edit: Oh yes I should say that this question was posted on other forums as well to let you know, sorry.


What good does that statement do. You didn't include links to the other postings so we can't check the answer on the other posting to see if we have the same suggestion.

Learn to use the forums properly, you've been asked once and still can't follow the suggestions.


There is links in the thread start to all places where this is discussed, everything that is said is not hard to find. (but just you have to visit two other forums, yes :/). And I keep all threads synced as good as I can.

But yes I do agree very much why cross-postings is messy and disrespectful for the people that spend their time helping you, even if you try to keep all threads synced. Sorry, I shall not repeat it like this.

Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28
Paul Clapham wrote:Unfortunately the rule is: Don't call a method which can be overridden from a constructor. If you do, then you risk the problem you had here, and it can happen even with no threading involved. Generally that means removing those method calls from the constructor and putting them in some new method which you would call after creating the object.


You mean that you don't risk suffer from NullpointerExceptions if you add (which is putted in a method we can call "start") after in main? I understand that its not because of the threading in this case you do so, but generally calling methods from above in a constructor. Well, if I understand what you said correctly.

Like this then maybe? As I didn't need any constructor I removed them to save space, the compiler add empty ones invisible.



Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28
EJP on Oracle forums wrote:I don't get the point of making it abstract in the base class and implementing it in the derived class when it is the base class that has the Thread member. That's just pointless complication. The main thing is to get Thread.start() away from the constructors and you've done that.


812278 on Oracle forums wrote:Oh ok, so the abstract start method isn't really necessary?



Optimized the code a bit I think.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Nullpointerexception when using a looping thread
 
Similar Threads
Qn on Threads
Why my threads are not synchronized properly ?
Implementing Comparable With a Generic Type
Object Casting help
Thread Synchronization