• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getGraphics() returns null

 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Derek Boring
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the problem, thanks for the help.
 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Derek what was the problem ? could you please share it i am facing the same
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neeraj jain wrote:@Derek what was the problem ? could you please share it i am facing the same


If by 'the same' you mean that getGraphics(0 of a Component is null, the solution is simple: don't use getGraphic(). Instead, do custom painting in the recommended way: http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html
 
Campbell Ritchie
Sheriff
Pie
Posts: 49861
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this discussion would fit better in our GUIs forum: moving.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic