• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized block

 
Deepa Nhattiala
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if the code is not reaching even till System.out.println("in synchronized block
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic