aspose file tools*
The moose likes Java in General and the fly likes synchronized block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "synchronized block" Watch "synchronized block" New topic
Author

synchronized block

Deepa Nhattiala
Greenhorn

Joined: Apr 06, 2013
Posts: 5
When does a try block having synchronized block throws NullPointerException?
Say the code snipet below is in a method:


try{
synchronized (oLock){
System.out.println("in synchronized block");
//Do something
}
}
catch(Exception e){
System.out.println("exception thrown");
e.printStackTrace();
}

Here oLock is initialised in the class like this:
static Object oLock = new Object();

If the above code snippet throws NullPointerException in catch block, what would be possible reasons for synchronized block to fail?
Carles Gasques
Ranch Hand

Joined: Apr 19, 2013
Posts: 199
    
    1
Hi,

I understand that you mean that a null pointer exception is printed in the catch block and is thrown in the try block.
Since the synchronized object seems to be initialized previously, it is static, I would risk to say that //Do something is the single point where a null pointer could be thrown.


Best regards,
Deepa Nhattiala
Greenhorn

Joined: Apr 06, 2013
Posts: 5
What if the code is not reaching even till System.out.println("in synchronized block
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
We would need to see the stack trace and how it relates to the code to be able to answer correctly. Please confirm that oLock does not point to null.
Deepa Nhattiala
Greenhorn

Joined: Apr 06, 2013
Posts: 5
oLock is declared and initialised as a class variable. The same class is having the method with this snippet. So, it can never be null, right? oLock will be initialised authomatically when the method is invoked, right?
Carles Gasques
Ranch Hand

Joined: Apr 19, 2013
Posts: 199
    
    1
Hi,

If you declare and initialize oLock as a class variable then it's already initialized when the method is invoked.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

Deepa, show us the complete code and copy and paste the stack trace of the exception, that would make it much easier to help you find out exactly what happens and why.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
What do you mean about class variable? Are you using a class field to hold a lock on an instance method? I suspect that will cause problems if you really are doing so.

But if oLock is not null, then something else must be null. You have not given us enough information to know what is null.
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

If lock is null it will definitely throw NullPointerException on a line with synchronized keyword.

Campbell Ritchie wrote:Are you using a class field to hold a lock on an instance method?

In which case holding a lock on a class var from instance method can lead to problem?
 
Don't get me started about those stupid light bulbs.
 
subject: synchronized block