Sign in to follow this  
Followers 0
Nicholas Kong

Occassional Null Pointer Exception in Java

2 posts in this topic

I know why null pointer exception happens in Java. An object is not created which means it is a null object reference.

 

Problem is, I am facing an occasional case of the null pointer exception that is harder to track down because

sometimes the error happens which means I need to re-compile my game and it would work just fine after that.

Other times, the error just won't happen.

 

Luckily I encountered it again and can copy

and paste what the Eclipse IDE is saying:

Exception in thread "main" java.lang.NullPointerException
    at sun.java2d.SunGraphics2D.validateColor(Unknown Source)
    at sun.java2d.SunGraphics2D.<init>(Unknown Source)
    at sun.awt.image.SunVolatileImage.createGraphics(Unknown Source)
    at java.awt.image.VolatileImage.getGraphics(Unknown Source)
    at java.awt.Component$BltBufferStrategy.showSubRegion(Unknown Source)
    at java.awt.Component$BltBufferStrategy.show(Unknown Source)
    at com.nicholaskong.zelda.MainMenuCanvas.flipBuffer(MainMenuCanvas.java:217)
    at com.nicholaskong.zelda.MainMenuCanvas.run(MainMenuCanvas.java:199)
    at com.nicholaskong.zelda.Main.main(Main.java:31)

There are two cases when the error happens:

booting up the game to display the main menu and main menu transition to gameplay.

It happens roughly once or twice during the days I run the game to test new features.

 

Is my bufferstrategy object null at some point?

The 3 errors seems to point to the code below.

private void flipBuffer(Graphics2D g) {
        // TODO Auto-generated method stub
        g.dispose();
        // null object of bufferStrategy object perhaps? compiler thinks so.
        strategy.show();
    }

But my strategy has an object reference.

Canvas create two buffers: one is a front buffer and the other is a back buffer.

Strategy object gets the bufferStrategy object.

  createBufferStrategy(2);
  strategy = getBufferStrategy();

Edited by warnexus
0

Share this post


Link to post
Share on other sites
My java is a bit rusty, but I believe you UI components are running in a thread separate from that of the main thread of execution. If so, the problem could be a race condition, and that would explain the intermittent occurrence of the exception.

Assuming this is the case, make sure that the objects which are causing the NPE are created before another thread which references them starts.

I may have missed something. I'm on my smartphone, so perhaps I didn't read your code and the error properly. But intermittent errors of this kind are consistent with a race condition. Hope that helps.
0

Share this post


Link to post
Share on other sites

My java is a bit rusty, but I believe you UI components are running in a thread separate from that of the main thread of execution. If so, the problem could be a race condition, and that would explain the intermittent occurrence of the exception.

Assuming this is the case, make sure that the objects which are causing the NPE are created before another thread which references them starts.

I may have missed something. I'm on my smartphone, so perhaps I didn't read your code and the error properly. But intermittent errors of this kind are consistent with a race condition. Hope that helps.

Thanks I will think about the race condition in relation to my code.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0