如何调试:线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException

时间:2011-03-17 01:11:40

标签: java eclipse nullpointerexception

我正在编写的程序是制作一个矩形的4个机器人,其中一条线指向北,南,东,西。有4个按钮(moveForward,moveBackward,turnLeft,turnRight)。

当我运行它时,我可以使用前进和后退按钮工作,但turnLeft和right不起作用。

我该如何解决?以及如何阅读调试器?

这是我点击按钮时得到的结果 (RobotBodyNorth - 点击turnRightButton)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at RobotBodyNorth.turnRight(RobotBodyNorth.java:55)
    at Robot.turnRight(Robot.java:40)
    at TurnRightButton.mouseClicked(TurnRightButton.java:21)
    at wheels.etc.DrawingPanel$MouseClick.mouseClicked(DrawingPanel.java:157)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

CODE:

public class Robot {
    private RobotBody _robotBody;

    public Robot (RobotBody robotBody){
        _robotBody = robotBody;
    }   
    public Color getColor(){
        return _robotBody.getColor();
    }
    public void moveBackward(){
        _robotBody.moveBackward();
    }
    public void moveForward(){
        _robotBody.moveForward();
    }
    public void moveLine() {
        _robotBody.moveLine();
    }
    public RobotBody turnLeft(){
        _robotBody = _robotBody.turnLeft();
        return _robotBody;
    }
    **public RobotBody turnRight(){
        _robotBody = _robotBody.turnRight();
        return _robotBody;
    }**
}

ROBOTBODY CLASS:

public abstract class RobotBody {
    protected Rectangle _rectangle;
    protected int _diameter, _centerX, _centerY;
    private Color _color;

    public RobotBody(Color color){
        _diameter = 30;
        _color = color;

        _rectangle = new Rectangle(_color){
            @Override
            public void mouseClicked( MouseEvent e ){
                RobotBody.this.mouseClicked( e );
            }
        };

        _rectangle.setColor(_color);
        _rectangle.setSize(_diameter,_diameter);
        _rectangle.setFrameThickness(1);
        _rectangle.setFrameColor(Color.BLACK);

         int x = (int)(400.0 * Math.random());
         int y = (int)(400.0 * Math.random());
            _centerX=x;
            _centerY=y;
            setCenter(_centerX,_centerY);
    }

    public RobotBody (RobotBody oldBody){   
        this._color = oldBody._color;
        this._diameter = oldBody._diameter;
        this.setCenter(oldBody._centerX, oldBody._centerY);
        getColor();

        _rectangle = new Rectangle(_color){
            @Override
            public void mouseClicked( MouseEvent e ){
                RobotBody.this.mouseClicked( e );
            }
        };
        _rectangle.setColor(_color);
        _rectangle.setSize(_diameter,_diameter);
        _rectangle.setFrameThickness(1);
        _rectangle.setFrameColor(Color.BLACK);
    }

    public Color getColor(){
        return _color;
    }

    public void setCenter(int x, int y) {
        _centerX=x;
        _centerY=y;
        _rectangle.setLocation(x-(_diameter/2),y-(_diameter/2));
    }

    public void mouseClicked( MouseEvent e ){
        int newX = (int)(400.0 * Math.random());
        int newY = (int)(400.0 * Math.random());
        setCenter( newX, newY );
        moveLine();
    }

    public abstract void moveLine();
    public abstract void moveBackward();
    public abstract void moveForward();
    public abstract RobotBody turnLeft();
    **public abstract RobotBody turnRight();**

    protected Color contrastWith( Color color ){
        if( colorLuminance( color ) < 0.5 )
            return Color.WHITE;
            return Color.BLACK;
    }
    protected double colorLuminance( Color color ){
        int    red   = color.getRed();
        int    green = color.getGreen();
        int    blue  = color.getBlue();
        double lum   = ((float)(red + green + blue)) / 765.0;
        return lum;
    }
}

ROBOTBODYNORTH CLASS:

public class RobotBodyNorth extends RobotBody{
    protected Rectangle _rectangle;
    private Line _line;
    private Color _color;
    private Color _c;

    public RobotBodyNorth(Color color){
        super(color);
        _line = new Line(_centerX,_centerY,_centerX,_centerY-(_diameter/2));
        _c=contrastWith(color);
        _line.setColor(_c);
    }

    public RobotBodyNorth(RobotBody oldBody){
        super(oldBody);
        _line = new Line(_centerX,_centerY,_centerX,_centerY-(_diameter/2));
        _c=contrastWith(_color);
        _line.setColor(_c);
    }

    public void moveBackward(){
        int _newCenterX = _centerX;
        int _newCenterY = _centerY + _diameter/2;
        setCenter( _newCenterX, _newCenterY);
        moveLine();
    }

    public void moveForward(){
        int _newCenterX= _centerX;
        int _newCenterY = _centerY - _diameter/2;
        setCenter( _newCenterX, _newCenterY);
        moveLine();
    }

    public RobotBody turnLeft(){
        _rectangle.hide();
        _line.hide();
        return new RobotBodyWest(this);
    }

    **public RobotBody turnRight(){
        _rectangle.hide();
        _line.hide();
        return new RobotBodyEast(this);
    }**

    @Override
    public void moveLine(){
        _line.setPoints(_centerX,_centerY,_centerX,_centerY-(_diameter/2));
    }
}

主要课程:

public class Main {

    public static void main(String[]args){
        new Arena();

        RobotBodyNorth r1 = new RobotBodyNorth(Color.RED);
        Robot North = new Robot(r1);
        new ControlButtons(0, 0, North);
    }
}

2 个答案:

答案 0 :(得分:2)

您似乎没有为_rectangle课程中的RobotBodyNorth分配值。由于我没有行号,我不能确定。 NullPointerException表示您尝试访问的变量中没有值,通常是当您尝试在其上调用方法时。

也可能是因为您已在Rectangle _rectangleRobotBody中声明了RobotBodyNorth。我相信Java隐藏了从RobotBody继承的变量。在为_rectangle RobotBody分配值时,_rectangle中的RobotBodyNorth不是。尝试删除_rectangle中的RobotBodyNorth,看看是否有帮助。

答案 1 :(得分:1)

您没有初始化_rectangleat RobotBodyNorth.turnRight(RobotBodyNorth.java:55)