The Source for Java Technology Collaboration

Home » java.net Forums » Java Desktop Technologies » Java 2D

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

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 1 - Last Post: Jul 26, 2007 10:05 AM by: Chris Campbell Threads: [ Previous | Next ]
Ken Warner
Why am I getting a null pointer exception when I try to get the drawGraphics from a BufferStrategy?
Posted: Jul 25, 2007 10:01 PM
  Click to reply to this thread Reply

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.<init>(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".


Chris Campbell
Re: [JAVA2D] Why am I getting a null pointer exception when I try to get the drawGraphics from a BufferStrategy?
Posted: Jul 26, 2007 10:05 AM   in response to: Ken Warner
  Click to reply to this thread Reply

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.<init>(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".





 XML java.net RSS