• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling the createImage() function

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is it that I get a java.lang.NullPointerException on the line:

Graphics gImage = offScreenImage.getGraphics();


When I use the following code:


import java.awt.*;
import javax.swing.*;

public class dispImage extends JFrame
{

Image offScreenImage;


public dispImage()
{
offScreenImage = createImage(800, 600);

this.setSize(200, 200);
this.show();
}


public void paint(Graphics g)
{
super.paint(g);

Graphics gImage = offScreenImage.getGraphics();
}


public static void main(String[] args)
{
dispImage d1 = new dispImage();
}
}


... and yet, if I move the createImage() call to inside the paint() function it works fine?

I really don't want to call the createImage() function every time the frame paints itself, as this will really slow things down. I just want to call it once at the start of the program, then use that one instance throughout the program. I know it's something simple but I can't work out what!

Thanks very much for any answers,


D Taylor
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi D.N.,

Welcome to JavaRanch!

Before the JFrame is shown on the screen, it has no graphics context yet -- i.e., during the JFrame constructor call. createImage just silently returns "null" under these conditions, so your offscreen graphics reference is null.

In paint(), just do this:



The cost you pay at the typical paint() call is a single comparison -- not too big a deal, I hope you'll agree.
 
D N Taylor
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Ernest!

Yeah, I was hoping it would be something as simple as that! Just after I posted I kinda figured out one way to do it - have a boolean flag set to false at the start, then set it to true in the paint() method after you've called createImage(). Then just test if the flag is true or false every time paint() is called.

But you posted the best way to do it, by simply testing if the image is null. It's a lot neater and tidier your way. Thanks very much!

D N Taylor
 
reply
    Bookmark Topic Watch Topic
  • New Topic