Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronized NullPointerException  RSS feed

 
puff li
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This statement



continuously throws NullPointerException

getSemphore() is a synchronized static method of CheckPoint and isBeforeWait() is a synchronized method of a Semphore object.

I am quite sure Semphore object is not null and I use Class.forName to load CheckPoint class before this statement and after the exception I print out the Semphore object, it's not null either. How come it throws NullPointerException? I'm driven mad! Please help!

Thanks.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some things to try:

Is this line the very first line in the stack trace you see?

Is it possible that you're running from .class files that are older than the corresponding .java files? You can get weird results like this if you don't fully recompile your files after editing them.

li hao wrote:I am quite sure Semphore object is not null and I use Class.forName to load CheckPoint class before this statement and after the exception I print out the Semphore object, it's not null either.

Are you sure that the non-null Semaphore you see is for the same thread that you're getting the NPE from? If you've got multiple threads going, you may be seeing an NPE from one thread and a non-null Semaphore in a different thread.

Is threadId constant? Is it a local variable? Is there any way it could possibly be changing while this code is running?
 
puff li
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying.

Below is the information printed



It's all from this statement. ThreadId is not constant. It is printed by
etc
And the semphore infomation is printed:

It's not null.

By the way, getSemphore() get the semphore object from a static hashmap from CheckPoint class. The hashmap is initialized in the class as static field. Other threads put key and value into it.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try replacing this line

with this code:

What is the output now?
 
puff li
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I found the bug. The Boolean object returned by this statement is null. I forgot to initialize it
Thanks anyway
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Thanks anyway"? It sounds like exactly the sort of bug that my print statements would have exposed for you.

Why use a Boolean wrapper at all, by the way? It seems like the primitive boolean type is much less error-prone here.
 
puff li
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right. I should use boolean...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!