• 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

getGraphics() returns null

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the problem, thanks for the help.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Derek what was the problem ? could you please share it i am facing the same
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this discussion would fit better in our GUIs forum: moving.
reply
    Bookmark Topic Watch Topic
  • New Topic