我有一个空指针异常,并且不知道如何解决它

时间:2014-08-05 03:45:46

标签: java swing nullpointerexception

我已经尝试过original Null Pointer Exception question,但它没有帮助。这是堆栈跟踪的第一行和第二行:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Client.SpriteField.paintComponent(SpriteField.java:30)

所以这告诉我它在包"客户",类" SpriteField"和方法" paintComponent"对?这是SpriteField类中的代码:

package Client;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

import javax.swing.JPanel;

public class SpriteField extends JPanel 
{
    RoundSprite mSprite;
    public void CreateSpriteAt(float tX, float tY, int tWidth, int tHeight, int tRotation) 
    {
        mSprite = new RoundSprite();
        mSprite.SetPosition(tX, tY);
        mSprite.SetSpriteWidth(tWidth);
        mSprite.SetSpriteHeight(tHeight);
        mSprite.SetSpriteRotation(tRotation);
    }
    public void paintComponent (Graphics g)
    {
        if(g != null)
        {
            super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        AffineTransform tOldTransform = g2.getTransform();

        mSprite.DrawSprite(g2);

        g2.setTransform(tOldTransform);
        }
    }

}

这可能只是一个不小心的错误,但我似乎无法找到它。请帮忙,谢谢你的时间。

编辑:如所问,这是主要方法:

final SpriteField mSpritePanel = new SpriteField();
mSpritePanel.addMouseListener(new MouseAdapter() 
{
    public void mouseClicked(MouseEvent e)
    {
        float tX = e.getX();
        float tY = e.getY();
        if(tTextWidth.getText() == null)
        {
            tTextWidth.setText("50");
        }
        int tIntWidth = Integer.parseInt(tTextWidth.getText());
        if(tIntWidth == 0)
        {
            tIntWidth = 50;
        }
        if(tTextHeight.getText() == null)
        {
            tTextHeight.setText("50");
        }
        int tIntHeight = Integer.parseInt(tTextHeight.getText());
        if(tIntHeight == 0)
        {
            tIntHeight = 50;
        }
        int tRotate = Integer.parseInt(tTextRotation.getText());
        mSpritePanel.CreateSpriteAt(tX, tY, tIntWidth, tIntHeight, tRotate); 
        mSpritePanel.repaint();
    }
});

2 个答案:

答案 0 :(得分:4)

这是问题

mSprite.DrawSprite(g2); // you need to initialize mSprite

但此处mSpritenull

通过调试可以轻松找到这种错误。

答案 1 :(得分:1)

在组件已经在屏幕上之后,您不会在很久以后调用CreateSpriteAt。第一次到达mSprite.DrawSprite(g2);时,它会因NPE而爆炸,因为mSprite字段最初为空。

简单的解决方法是在变量为null时不尝试绘制精灵:

if (mSprite != null) mSprite.DrawSprite(g2);

但是,既然你已经命名了这个类SpriteField,那么精灵列表是你真正想要的,而不是一个精灵?

public class SpriteField extends JPanel {
    final List<RoundSprite> sprites = new ArrayList<>();

    public void CreateSpriteAt(float tX, float tY, int tWidth, int tHeight, int tRotation) {
        RoundSprite s = new RoundSprite();
        s.SetPosition(tX, tY);
        s.SetSpriteWidth(tWidth);
        s.SetSpriteHeight(tHeight);
        s.SetSpriteRotation(tRotation);
        sprites.add(s);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        AffineTransform originalTransform = g2.getTransform();

        for (RoundSprite s : sprites) {
            s.DrawSprite(g2);
            g2.setTransform(originalTransform);
        }
    }
}

P.S。请考虑遵循标准Java命名约定!