Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

can the following code produces Null pointer exception?‏  RSS feed

 
khan gul
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a question on the following code. Is it possible that the following code can throw Null Pointer Exception?


This Test class is been added to ByValue class as a static variable.




Since Test class is being added dynamically to ByValue class and Test is a static field in the ByValue, is it possible that when one Thread RESET this static Test field the other thread sees a NULL value?

From Time line point of view, say Thread T3 run() first and instantiate ByValue first and then set Test object to static Test field. But before calling byValue.getTest(), Thread T4 run() and reset a new Test object in the ByValue. Question I have is it possible that T3 can get Null Pointer Exception when it calls byValue.getTest().getName();

Thanks,

[Edit - added code tags - MB]
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please edit your post and UseCodeTags.
 
khan gul
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked the code and it has [code:java] tag. Is it fine now?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. The static Test variable cannot go back to null. It is null when the class is first initialized. Then you set it to some non-null value. It will never be null again unless you explicitly set it to null, which, as far as I can see, you're not doing.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed. The code is not thread-safe and it's difficult to predict exactly what it will do. But getting a NullPointerException from the ByValue.test field does not appear to be possible.

By the way, you're making it very difficult to debug any problems, if they occur:

Calling getClass() has no observable effect. Nothing is printed. You're effectively hiding any exception, if it occurs, such that you will have no idea what the problem is. I strongly suggest printing the stack trace, or at least the toString() value of the exception. There's very useful information in there, which you are currently cut off from.
 
Nomaan Butt
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since Test class is being added dynamically to ByValue class and Test is a static field in the ByValue, is it possible that when one Thread RESET this static Test field the other thread sees a NULL value?


dynamic initiation has nothing to do with your query.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!