File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes NullPointerException With synchronize block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "NullPointerException With synchronize block" Watch "NullPointerException With synchronize block" New topic

NullPointerException With synchronize block

jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306
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

Joined: Jan 30, 2000
Posts: 18671
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.

"I'm not back." - Bill Harding, Twister
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306
sure we will let you know what we find.
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306
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

Joined: Nov 09, 2010
Posts: 5
Same one I am facing


Have anyone gone through this ?

Thanks in advance
Roberto Perillo

Joined: Dec 28, 2007
Posts: 2271

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.

Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
I agree. Here's the link:
subject: NullPointerException With synchronize block
It's not a secret anymore!