[java] JFrames and Graphics

Started by
15 comments, last by Pajari 18 years, 9 months ago
I am having one hell of a time writing a simple strategy game in Java (actually not a full game, just a tech demo). The problem seems to come from drawImage(). I attempt to draw an image (or a hundred of them- it's a tile-based game and I iterate over a matrix of terrain tiles and display them in order) and I get a null pointer exception. g.drawImage(grass, 40, 40, this); gives me a null-pointer exception? The only thing that could possibly be null would be the last argument, "this", or the component that I'm supposed to draw on. I'm using a single JFrame for my game (at least right now) and I was wondering if there were any special or strange conventions with JFrames that I wasn't aware of, and that can make an error like this occur. Any help at all would be greatly apprecieated- this is driving me up the wall.
Advertisement
Could you post the stack trace?

catch(Exception e){e.printStackTrace();};
I'm pretty sure that if g.drawImage(grass, 40, 40, this); is throwing an NPE, that means your Graphics object g is null, not the parameters you're using.
Quote:Original post by Pajari
g.drawImage(grass, 40, 40, this); gives me a null-pointer exception? The only thing that could possibly be null would be the last argument, "this", or the component that I'm supposed to draw on.

There is no way "this" is null. What could be null is either "g" or "grass".

shmoove
I've had this problem too. One way around this is to allow your frame-making class to extend the Canvas class and do your drawImage() in the overridden paint(). Other than that, I'd suggest looking in the documentation for methods that can create a Graphics object for a JFrame, since Graphics is abstract and cannot be instantiated.

public class demo extends Canvas implements Runnable{  JFrame jf;  Thread thr;  public void paint(Graphics g) {  //do your image drawing here  }  public void run() {    while(true) {      repaint();    }  }  public static void main(String[] args) {  thr = new Thread(this);  thr.start();  jf = new JFrame();  thr.start();  jf.add(this);                 //adds Canvas object to window  jf.pack();  jf.setVisible(true);  }}
Thanks for the help, everyone. Like I said, it's most apprecieated.

I'm fairly sure that my Graphics object is instantiated and that my images are ok. And I always print the stack trace when I catch an exception- that's how I found out that something in g.drawImage() was null. It did not show up at compile-time as an error (I'm using JCreator 2.5 or something, btw).

And as for shinobi's response, I was thinking about using a canvas before (it would make handling mouse events easier too) and now it looks like that will be the best solution. I've never used threads before, so this should be extremely interesting.

Thanks! I'll post the results later.

[Edited by - Pajari on June 19, 2005 10:41:20 AM]
Now I'm getting a null pointer exception with the Canvas- even after I've created it and gotten the accessible context for it. Any help?
Try to do a setVisible(true) BEFORE you do all those fancy operations.
I recommend finding out what is really null. To do this, enable assertions at the command line with -ea, then assert pretty much everything.

assert(g != null);
assert(tile != null);

etc, etc...

It is kind of a waste of time to switch to a different solution when you don't even know what the problem is.
Oh, and I would recommend using a really good IDE if the one you have stinks. Eclipse 3.1 RC2 and NetBeans 4.1 are both good and free. I am using Eclipse, and it is very nice. It lets you follow the stack trace when you get an exception.

This topic is closed to new replies.

Advertisement