Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getGraphics() null but paint() still works!

 
Sara Savoie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I am currently working on an image viewer program and am having a bit of difficulty displaying the Image objects themselves. Basically, I created an ImagePanel class with a paintComponent(Graphics g) method. When I use this, the image does in fact show up in the ImagePanel panel, but I am still getting a null pointer exception and a stack trace. I have read a few threads on the getGraphics() returning null issue, but the thing is that the ImagePanel IS added to a JFrame which is packed and set to visible. It's heartening that it's showing the image but the stack trace is worrying!

My code is pretty massive so here are some relevant bits:

From the PhotoGUI class:





Any help or suggestions would be VERY much appreciated!
Thanks in advance.
-Sara
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we see the complete exception stack trace?
 
Sara Savoie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, thanks a lot for your reply Swastik, but I just fixed it!

For anyone that does come across this in the future, all I had to do was replace the getGraphics() parameter with viewPane.getGraphics(), where viewPane is an instance of ImagePanel. Gotta love a simple solution.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our GUIs forum, where we usually discuss that sort of question.
 
Rob Camick
Ranch Hand
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sara Savoie wrote:For anyone that does come across this in the future, all I had to do was replace the getGraphics() parameter with viewPane.getGraphics(), where viewPane is an instance of ImagePanel. Gotta love a simple solution.


For anybody reading this that is NOT the proper solution. You should never (in my experiences) use the getGraphics() method.

The proper solution is to modify the the setImage() method to be:



In fact given that the ImagePanel paint the image at it original size there is even no need to create a custom component.

The easier solution is to use a JLable and and ImageIcon. Then to change the image you change the ImageIcon and everything is repainted automatically.
 
Sara Savoie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

Thank you for your reply. I have tried your method but now it seems to be printing a stack trace and not painting anything at all!

To elaborate, we have to create a custom "Photo" object- the details are irrelevant but it does have a getImage() method that returns an image object. Below is how I went about implementing your suggestion:





My stack trace is as follows:

java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at ca.ubc.cs.cpsc211.photo.PhotoGUI.displayPhoto(PhotoGUI.java:289) //on this line: imageSpace.setIcon(new ImageIcon(p.getImage()));
at ca.ubc.cs.cpsc211.photo.PhotoGUIDriver.main(PhotoGUIDriver.java:12) //on this line: gui.displayPhoto(p);

Any further help is much appreciated. Thanks again.
 
Rob Camick
Ranch Hand
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in your Photo class and I have no idea what is does. It looks like you have a null image.

In your original code you had:



You new code is:

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic