Arturas M
Arturas M

Reputation: 4369

Why am I getting null exception here?

Well, it seems pretty strange. I'm kind of initialiing the variable and it works in the constructor right after the initialization, however in the draw method it doesn't work anymore...

Well here's the code and I'm getting the exception whenever I try to call any method of the object position in the method draw():

public class GlText extends GraphicObject {

String fullText = "";

public GlText(Bounds bounds) {
    this.bounds = bounds;
    this.position = getPosition();
    System.out.println("BLOGAS: " + position.getX());
}

@Override
public void draw(Graphics g, AlignStrategy align) {
    g.setColor(Color.orange);
    g.setFont(new Font("Arial", Font.BOLD, 36));

    FontMetrics metrics = g.getFontMetrics();
    int textHeight = metrics.getHeight();
    int textWidth = metrics.stringWidth(fullText);

    // drawing position is recalculated according to text size, etc...
    int x = 0;
    int y = 0;

    // calculating x according to text width



    // !!!!!!!!!!!!!!! the Null pointer exception happens in the next line:
    System.out.println("POSITION " + position.getX());**
    System.out.println("TEXTWIDTH " + textWidth);
    System.out.println("BOUNDS " + bounds.getX());
    if (position.getX() - textWidth < bounds.getX())
        x = bounds.getX();
    else
        x = position.getX() - textWidth;

    // calculating y according to text height
    if (position.getY() - textHeight < bounds.getY())
        y = bounds.getY();
    else
        y = position.getY() - textHeight;


    Bounds drawPos = new Bounds(x, y, textWidth, textHeight);       

    // ADDED ALIGN STRATEGY
    Bounds alignedPosition = (Bounds) align.getAligned(drawPos, bounds);

    g.drawString(fullText, alignedPosition.getX(), alignedPosition.getY());


}

public final Bounds getPosition() {

        int x = 0;
        int y = 0;
        Random random = new Random();
        random.setSeed(System.nanoTime());

        x = bounds.getX() + random.nextInt(bounds.getWidth());
        y = bounds.getY() + random.nextInt(bounds.getHeight());

        Bounds newPosition = new Bounds(x, y, 0, 0);
        return newPosition;


}

Here's how the exception looks like:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GraphicObjectPkg.GlText.draw(GlText.java:43)

And here's my abstract class which I'm extending with GlText:

public abstract class GraphicObject {
    protected Bounds bounds = new Bounds();
    protected Bounds position = null;

    public abstract void draw(Graphics g, AlignStrategy align);
}

OK, this is the place where the constructor is called, hmm, it's called from outside and yet the constructo does print the line "BLOGAS: 180":

GlText myText = new GlText(currentBounds);
        myText.setFullText("gerai");
        mainGroup.addChild(myText);

Final Edit: Thanks everybody for helping me, due to your help, I finally identified the problem and it was this leftover:

public void setFullText(String fullText) {
    this.fullText = fullText;
    position = null;
}

I had modified the class and I totally forgot this method had such thing... I found this by using the find command, so I guess the moral of the story for me this time is that whether it seems like you haven't created any other variables, it's better to test it with the find function on the editor...

Upvotes: 0

Views: 194

Answers (1)

Amit Deshpande
Amit Deshpande

Reputation: 19185

If draw is a callback event and if the framework doesn't use your constructed object then you will get NullPointerException.

You need to ensure framework uses passed object while calling draw method you can simply do it using == operator in draw method.

Store the reference in the constructor to a object of GlText

private final GlText customized = null;
//Inside Constructor 
 customized = this;

Inside draw method

 if(this != customized )
 {
    System.out.println("Objects are different");
 }

Hardway is to understand framework and check how draw method is getting called:-)

Upvotes: 1

Related Questions