aspose file tools*
The moose likes Beginning Java and the fly likes getGraphics() returns null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getGraphics() returns null" Watch "getGraphics() returns null" New topic
Author

getGraphics() returns null

Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 63
The getGraphics() method in my constructor is returning a nullPointerException and I can't figure out why. This class is extending a JPanel and I thought calling super() would fix it because it makes sense that the JPanel would initialize its graphics object in the constructor, but that didn't fix it. The line that produces the error is in bold. I've narrowed it down to the getGraphics() method is returning null, so the next line that calls the graphics object is actually the one that produces the error.

Can someone please help me figure out why this is returning null?



P.S. print(String) is a method I made that draws lines of text on the graphics object, similar to System.out.println(String) in the command prompt.

[ August 23, 2007: Message edited by: Derek Boring ]
[ August 23, 2007: Message edited by: Derek Boring ]

Life is 10% what happens and 90% how you react to it.
Attitude is everything.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The Graphics object will be null until something is visible. This is demonstrated below.

(Note: If there is no explicit call to this or super as the first line of a constructor, then there is an implicit call to super with no arguments. So explicitly adding super() should not make a difference.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 63
The HTMLFrame is shown inside a JScrollPane which is inside a JTabbedPane which is on a JFrame that is visible. (hope that made sense) The JFrame shows, but nothing shows on it and I still get a null pointer exception at the same place. The JFrame is shown before anything is added to it.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Derek Boring:
The HTMLFrame is shown inside a JScrollPane which is inside a JTabbedPane which is on a JFrame that is visible. (hope that made sense) The JFrame shows, but nothing shows on it and I still get a null pointer exception at the same place. The JFrame is shown before anything is added to it.

I think the JPanel itself (the HTMLFrame) needs to be visible at the time getGraphics is called.
Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 63
I found the problem, thanks for the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getGraphics() returns null
 
Similar Threads
prints
No margins in Netscape?
Static question
getGraphics() return null
JasperReports: "Error loading object from file" Exception