Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would I get a NullPointerException on a Synchronized Lock?

I have just a "Simple" Synchronized Lock Block as such:

private final Object screenLock = new Object();

public void Update(double deltaTime)
{
    synchronized (screenLock)
    {
        screenManager.Update(deltaTime);
    }
}

public void Draw()
{
    synchronized (screenLock)
    {
        screenManager.Draw();
    }
}

deltaTime isn't null, screenManager isn't null, so I'm a little lost.

I know that I shouldn't be putting a lock on such a High level, but the problem persists wherever I put the Block. I have two threads running to Update and Draw both with screenLock.

What would cause the NullPointerException on the lock?

Trace:

java.lang.NullPointerException at com.dnx.manavo.ScreenManager.Update(ScreenManager.java:107) at com.dnx.manavo.ApplicationActivity.Update(ApplicationActivity.java:98) at com.dnx.manavo.GameThread.run(GameThread.java:43)

like image 665
Nate-Wilkins Avatar asked Aug 30 '12 02:08

Nate-Wilkins


1 Answers

Thanks to @Adam Batkin, and @MadProgrammer - I found my specific NullPointerException within some parts of the screenManager

I did some research and sometimes a NullPointerException will be thrown at the begining of the Synchronized Block, but in fact its being thrown within the Block.

Other Reasons:

  • screenLock is null
  • Code inside your Synchronized Block is throwing an Exception
like image 107
Nate-Wilkins Avatar answered Nov 15 '22 00:11

Nate-Wilkins