为什么我在这里得到null异常?

时间:2012-09-30 12:01:12

标签: java nullpointerexception

嗯,这看起来很奇怪。我有点初始化变量,它在初始化后立即在构造函数中工作,但是在draw方法中它不再起作用了......

这里是代码,每当我尝试在方法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;


}

以下是异常的样子:

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

这是我用GlText扩展的抽象类:

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

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

好的,这是调用构造函数的地方,嗯,它是从外部调用的,但是constructo确实打印了行“BLOGAS:180”:

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

最终编辑: 感谢大家帮助我,由于你的帮助,我终于找到了问题,这就是剩下的:

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

我修改了这个类,我完全忘记了这个方法有这样的事情......我通过使用find命令找到了这个,所以我猜这个故事对我来说是道德的,不管它看起来好像你没有'创建了任何其他变量,最好使用编辑器中的find函数进行测试...

1 个答案:

答案 0 :(得分:1)

如果draw是一个回调事件,如果框架没有使用你构造的对象,那么你将获得NullPointerException

您需要确保框架在调用draw方法时使用传递的对象,您可以使用==方法中的draw运算符来执行此操作。

将构造函数中的引用存储到GlText

的对象中
private final GlText customized = null;
//Inside Constructor 
 customized = this;

内部draw方法

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

Hardway是理解框架并检查draw方法的调用方式:-)