Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4072180
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:51:08+00:00 2026-05-20T16:51:08+00:00

The program I’m writting is to make 4 robot with a rectangular shape with

  • 0

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)
    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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:51:09+00:00Added an answer on May 20, 2026 at 4:51 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Program followed by output. Someone please explain to me why 10,000,000 milliseconds from Jan
Most program languages have some kind of exception handling; some languages have return codes,
My program generates relatively simple PDF documents on request, but I'm having trouble with
A program receives a list of Messages (base type). Each message in the list
My Program overrides public void paint(Graphics g, int x, int y); in order to
What program can I use to decompile a class file? Will I actually get
Strange program hang, what does this mean in debug? After attaching windbg I found
The program that I am currently assigned to has a requirement that I copy
A program that I work on assumes that the UUID generated by the Windows
My program has to read files that use various encodings. They may be ANSI,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.