The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
May 2000



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


drawImage/createImage problem

Posted by Adam W on May 08, 2000 at 11:20 AM

I've been trying to get rid of NullPointerEx. here for about 2 days!
First it came up at createImage() which was returning Null.
Adding a call to show() solved that one but, there is now
a NullPointerEx. at drawImage(). I got it working once! This code
seems to work fine under Windows, but not Solaris.


public void draw(Graphics g)
{ //set screen colours
Color bg = Color.white;
Color fg =;
Color cartColor = new Color(0,20,255);
Color poleColor = new Color(100,100,0);
Color arrowColor = new Color(255,255,0);
Color trackColor = new Color(100,100,50);

//get window size
Dimension d = size();
//System.out.println( d );

// actual screen position of cart
int posx=(int)(d.width/2-sim.x*pixelspermeter);
int posy=(d.height/2-cartheight);

//Create the offscreen graphics context, if no good one exists.
|| (d.width!=offDimension.width)
|| (d.height!=offDimension.height) ) {

show(); // needed to stop NullPointerEx. from createImage
offDimension = d;

offImage = createImage(d.width, d.height); // returning null ??

if( offImage == null ) System.out.println( "offImage == null" );
System.out.println( "\n*** 1 ***" );
offGraphics = offImage.getGraphics();
if( offGraphics == null ) System.out.println( "offGraphics == null" );
System.out.println( "*** 2 ***" );

//Erase the previous image.
offGraphics.clearRect(0, 0, d.width, d.height);

//... drawing to offGraphics ...

//Last thing: Paint the image onto the screen.
System.out.println( "------------>" );
boolean b = g.drawImage(offImage, 0, 0, this);
System.out.println( "drawImage = "+b );


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us