• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException With synchronize block

 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this sounds really odd..

I just confirmed that the following code sometimes gives a nullpointerexception at line 2.



the variable c is not null. Isn't the cpu supposed to wait on line 2 until the lock on object refered by variable c is released? Why is it throwing a NullPointerException instead?

We are using version 1.4 of java supplied by ibm along with wsad.

Any response will be appreciated.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume "synchronize" is actually "synchronized". Otherwise you may have a method call to a method called synchronize(), and the problem is there.

I think the most likely scenario here is that the source file is out of sync with the class file, and the line number is incorrect. Try adding some print statements before and after the syncrhonized statement. This way (a) you should see the output change, and (b) you should see the line number change. If either of these does not happen, the problem is probably not with the sync block.

Less likely, there could be a bug of some sort with the particular compiler or JVM you're using.

It might be worthwhile to make the variable c final, to see if that makes a difference. Or even, get rid of the variable, and just call synchronzied(A.class). There's no good reason why that should make a difference, but then, there's no good reason this problem should be happening at all, so you might as well try different things.

Please let us know if you find the problem, as now I'm curious.
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sure we will let you know what we find.
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We found the issue.

It was a debugger issue. The debugger was treating the entire synchronized block as one statement. When we stepped through the code, the entire block was executed but the debugger only shown the first statement as executing.

The nullpointer happened somewhere inside the synchronization block.

It was not a code synchronization problem.
 
sagar chandgude
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same one I am facing

??

Have anyone gone through this ?

Thanks in advance
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Sagar!

Champ, this thread is sort of old... how about opening a brand new thread to show us your problem? When you do so, please TellTheDetails, PostRealCode and UseCodeTags.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic