Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

null pointer when i try getGraphics()

800386May 23 2010 — edited May 29 2010
heys all i'm having some problems making this code run without exceptions.
boolean text_centered;
String text;
    public void CenterText() {
        text_centered = true;
        text = getText();
        java.awt.Graphics graphics = getGraphics();
        System.out.println(graphics.toString());
        java.awt.FontMetrics metrics = graphics.getFontMetrics(getFont());
        java.awt.geom.Rectangle2D rectangle = metrics.getStringBounds(text, graphics);
        text_left = (int) ((getWidth() - rectangle.getWidth()) / 2);
        text_top = (int) ((getHeight() - rectangle.getHeight()) / 2) + metrics.getAscent();
        graphics.dispose();
        setText("");
    }
"public Graphics getGraphics(): Creates a graphics context for this component. This method will return null if this component is currently not displayable. "

how do we make a component displayable?

Edited by: Pacerier on May 23, 2010 3:09 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 26 2010
Added on May 23 2010
4 comments
92 views