【问题标题】:How to debug : Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException如何调试:线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException
【发布时间】:2011-03-17 01:11:39
【问题描述】:

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

当我运行它时,我可以让前进和后退按钮工作,但左转和右转不起作用。

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

这是我单击按钮时得到的 (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)

代码:

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;
    }**
}

机器人类:

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 类:

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);
    }
}

【问题讨论】:

    标签: java eclipse nullpointerexception


    【解决方案1】:

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

    也可能是因为您在RobotBodyRobotBodyNorth 中都声明了Rectangle _rectangle。我相信 Java 隐藏了从RobotBody 继承的变量。虽然RobotBody 中的_rectangle 被分配了一个值,但RobotBodyNorth 中的_rectangle 却没有。尝试删除RobotBodyNorth 中的_rectangle,看看是否有帮助。

    【讨论】:

    • 我尝试从 RobotBodyNorth 中删除 it_rectangle 但没有改变。我认为在 RobotBodyNorth 中,矩形被分配了一个来自 super(color) 的值。 public RobotBody turnRight(){ _rectangle.hide(); _line.hide();返回新的 RobotBodyEast(this);我认为 (this) 将创建一个新的 RobotBodyEast 实例,该实例最终将成为 this* RobotBodyNorth 实例的副本,并从此方法返回该新实例。
    • 好吧,我不知道你的RobotBodyEast 类是做什么的,但问题在于_rectangle.hide(),因为_rectangle 没有值,这就是NullPointerException你得到的是来自。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-21
      • 2013-12-09
      • 1970-01-01
      • 2011-02-03
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多