Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

can the following code produces Null pointer exception?‏

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please edit your post and UseCodeTags.
 
khan gul
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4055
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic