Skip to main content

Why am I getting a null pointer exception when I try to get the drawGraphics from a BufferStrategy?

1 reply [Last post]
Anonymous

Environment: Java 1.5.0.12; Win2K SP4

Every once in a while, if I stop or restart the applet at just the wrong time
I see the following exception. Line marked Here >> is the last code executed
in my stuff. I check to see if the BufferStrategy is null before trying to
get a new drawGraphics. It never says it's null.

It's annoying. I didn't begin to get these errors until I recoded to use
the canonical render loop for BufferStrategy.

java.lang.NullPointerException
at sun.java2d.SunGraphics2D.(SunGraphics2D.java:213)
at sun.awt.image.SunVolatileImage.createGraphics(SunVolatileImage.java:174)
at java.awt.image.VolatileImage.getGraphics(VolatileImage.java:224)
at java.awt.Component$BltBufferStrategy.getDrawGraphics(Component.java:3563)
Here >> at pancyl.PanCylCanvas.newPixels(PanCylCanvas.java:243)
at pancyl.PanCylProjector.interpolateBC(PanCylProjector.java:749)
at pancyl.PanCylProjector.setImageParametersAndPixels(PanCylProjector.java:316)
at pancyl.PanCylImageFetch.run(PanCylImageFetch.java:418)
at java.lang.Thread.run(Thread.java:595)

private void checkBufferStrategy()
{
if(bs == null)
{
System.err.println("BUFFER STRATEGY NULL...");
this.createBufferStrategy(2);
bs = this.getBufferStrategy();
}

}
private boolean DRAWATT = false;
public void newPixels()
{
//System.err.println("newPixels()...");
try
{
checkBufferStrategy();
do {
do {
Exception here -> bg = (Graphics2D)bs.getDrawGraphics(); //this is line 243
//System.err.println("Get BufferGraphics...");
mis.newPixels(pixels, cm, 0, thisW);
bg.drawImage(canvasImage, 0, 0, thisW, thisH, this);
if(DRAWATT)
drawAttribution();
bg.dispose();
//System.err.println("Dispose BufferGraphics...");
} while (bs.contentsRestored());
bs.show();
} while (bs.contentsLost());
}
catch (Exception ie)
{
ie.printStackTrace();
}
this.requestFocusInWindow();
}

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Chris Campbell

Is this reproducible on JDK 6? We made a bunch of fixes to
BufferStrategy and related code in JDK 6, so this may just be an old
bug.

Chris

On Jul 25, 2007, at 10:01 PM, Ken Warner wrote:
> Environment: Java 1.5.0.12; Win2K SP4
>
> Every once in a while, if I stop or restart the applet at just the
> wrong time
> I see the following exception. Line marked Here >> is the last
> code executed
> in my stuff. I check to see if the BufferStrategy is null before
> trying to
> get a new drawGraphics. It never says it's null.
>
> It's annoying. I didn't begin to get these errors until I recoded
> to use
> the canonical render loop for BufferStrategy.
>
> java.lang.NullPointerException
> at sun.java2d.SunGraphics2D.(SunGraphics2D.java:213)
> at sun.awt.image.SunVolatileImage.createGraphics
> (SunVolatileImage.java:174)
> at java.awt.image.VolatileImage.getGraphics
> (VolatileImage.java:224)
> at java.awt.Component$BltBufferStrategy.getDrawGraphics
> (Component.java:3563)
> Here >> at pancyl.PanCylCanvas.newPixels(PanCylCanvas.java:243)
> at pancyl.PanCylProjector.interpolateBC(PanCylProjector.java:
> 749)
> at pancyl.PanCylProjector.setImageParametersAndPixels
> (PanCylProjector.java:316)
> at pancyl.PanCylImageFetch.run(PanCylImageFetch.java:418)
> at java.lang.Thread.run(Thread.java:595)
>
> private void checkBufferStrategy()
> {
> if(bs == null)
> {
> System.err.println("BUFFER STRATEGY NULL...");
> this.createBufferStrategy(2);
> bs = this.getBufferStrategy();
> }
>
> }
> private boolean DRAWATT = false;
> public void newPixels()
> {
> //System.err.println("newPixels()...");
> try
> {
> checkBufferStrategy();
> do {
> do {
> Exception here -> bg = (Graphics2D)bs.getDrawGraphics(); //
> this is line 243
> //System.err.println("Get BufferGraphics...");
> mis.newPixels(pixels, cm, 0, thisW);
> bg.drawImage(canvasImage, 0, 0, thisW, thisH,
> this);
> if(DRAWATT)
> drawAttribution();
> bg.dispose();
> //System.err.println("Dispose BufferGraphics...");
> } while (bs.contentsRestored());
> bs.show();
> } while (bs.contentsLost());
> }
> catch (Exception ie)
> {
> ie.printStackTrace();
> }
> this.requestFocusInWindow();
> }
>
> ======================================================================
> =====
> To unsubscribe, send email to listserv@java.sun.com and include in
> the body
> of the message "signoff JAVA2D-INTEREST". For general help, send
> email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".