How to debug : Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
The program I'm writting is to make 4 robot with a rectangular shape with a line pointing north,south,east,west. There are 4 buttons (moveForward,moveBackward,turnLeft,turnRight).
When I run it, I can get the moveforward and backward button to work but the turnLeft and right does not work.
How do I fix it? and How to I read the debugger?
This is what I get when I click the button (RobotBodyNorth - clicked the 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)
开发者_C百科 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));
}
}
MAIN CLASS:
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);
}
}
It looks like you aren't assigning a value to _rectangle
in the RobotBodyNorth
class. Since I don't have line numbers, I can't be sure though. NullPointerException
means that there is no value in the variable you are trying to access, usually when you try to call a method on it.
It may also be because you have declared a Rectangle _rectangle
in both RobotBody
and RobotBodyNorth
. I believe Java is hiding the variable inherited from RobotBody
. While the _rectangle
in RobotBody
is being assigned a value, the _rectangle
in RobotBodyNorth
is not. Try removing the _rectangle
in RobotBodyNorth
and see if that helps.
You didn't initialize _rectangle
. at RobotBodyNorth.turnRight(RobotBodyNorth.java:55)
精彩评论