Jump to content

  • Log In with Google      Sign In   
  • Create Account

Occassional Null Pointer Exception in Java


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 warnexus   Prime Members   -  Reputation: 1503

Like
0Likes
Like

Posted 13 September 2013 - 03:34 PM

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, 13 September 2013 - 03:37 PM.


Sponsor:

#2 thok   Members   -  Reputation: 693

Like
0Likes
Like

Posted 14 September 2013 - 06:37 AM

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.

#3 warnexus   Prime Members   -  Reputation: 1503

Like
0Likes
Like

Posted 15 September 2013 - 12:50 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS