0

I'm getting an error that says java.lang.NullPointerException: null from a code whenever I run it.

Here is the code:

public class APRectangle
{
    private APPoint myTopLeft;
    private APPoint myTopRight;
    private APPoint myBottomLeft;
    private APPoint myBottomRight;
    private double  myWidth;
    private double  myHeight;

    public APRectangle( APPoint topLeft, double width, double height )
    {
        myTopLeft = topLeft;
        myWidth = width;
        myHeight = height;
    }

    public APPoint getTopLeft()
    {
        return myTopLeft;
    }

    public void setTopLeft( APPoint TL )
    {
        myTopLeft = TL;
    }

    public double getWidth()
    {
        return myWidth;
    }

    public void setWidth( double W )
    {
        myWidth = W;
    }

    public double getHeight()
    {
        return myHeight;
    }

    public void setHeight( double H )
    {
        myHeight = H;
    }

    public APPoint getTopRight()
    {
        return new APPoint( myTopLeft.getX() + myWidth, myTopLeft.getY() );
    }

    public APPoint getBottomLeft()
    {
        return new APPoint( myTopLeft.getX(), myTopLeft.getY() - myHeight );
    }

    public APPoint getBottomRight()
    {
        return new APPoint( myTopRight.getX(), myTopRight.getY() - myHeight );
    }
}

The very last method is the one that is giving me the error.

Here is my main class:

public class MainClass
{
 public MainClass()
 {
    }

 public static String printAPPoint( APPoint p )
 {
     return "(" + p.getX() + "," + p.getY() + ")";
    }

 public static String printAPRectangle( APRectangle R)
 {
     return "[APRectangle " + printAPPoint(R.getTopLeft()) +
            " " + R.getWidth() + "," + R.getHeight() + "]" ;
 }

  public static String printTopLeft( APRectangle R )
 {
     return "(Top Left is " + printAPPoint(R.getTopLeft()) + ")" ;
    }

 public static String printTopRight( APRectangle R )
 {
     return "(Top Right is " + printAPPoint(R.getTopRight()) + ")" ;
    }

public static String printBottomLeft( APRectangle R )
{
    return "(Bottom Left is " + printAPPoint(R.getBottomLeft()) + ")";
}

public static String printBottomRight( APRectangle R )
{
    return "(Bottom Right is " + printAPPoint(R.getBottomRight()) + ")";
}

 public static void main(String[] args)
 {
     APPoint p = new APPoint(1.0, 5.0 );
     APRectangle R = new APRectangle( p, 5.0, 3.0);
     System.out.println(printAPRectangle(R));
     System.out.println(printTopLeft(R));
     System.out.println(printTopRight(R));
     System.out.println(printBottomLeft(R));
     System.out.println(printBottomRight(R));
     System.out.println( "Done!" );
  }

}

The three methods before the last one work perfectly well, but I don't know why the last one isn't working. Can someone help me please?

Thanks, Rohan

2
  • Where do you get the exception? Commented Jul 23, 2012 at 3:13
  • When I run the main class after compiling everything, I get the exception saying java.lang.NullPointerException at APRectangle.getBottomRight(APRectangle.java:59) at MainClass.printBottomRight(MainClass.java:35) at MainClass.main(MainClass.java:46) I dont really know what that means Commented Jul 23, 2012 at 3:16

5 Answers 5

2

You forgot to initialize myTopRight, myBottomLeft, and myBottomRight. The other methods work fine because you don't use their actual points (but you probably should). AP Computer Science?

By the way, your exception message is saying this: We have a problem because one of our objects doesn't exist. We found the problem when we tried APRectangle.getBottomRight (line 59). We were doing APRectangle.getBottomRight because MainClass.printBottomRight (line 35) told us to. We were doing MainClass.printBottomRight because MainClass.main (line 46) told us to. Hope this clears things up.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes AP CS. So if I have to initialize all of those, then do I have to write modifier methods for it? What exactly should I do?
To eliminate the NullPointerException, you don't need to write modifier methods for them. You just need to initialize them in your constructor just as you initialized myTopLeft.
I solved my problem by editing the getBottomRight to public APPoint getBottomRight() { return new APPoint( myTopLeft.getX() + myWidth, myTopLeft.getY() - myHeight ); } that way it uses a value that was already initialized ;)
You don't need modifier methods, that's just something you should get in the habit of doing. Instead, use what you have in the accessor methods and put that in the constructor. That way you can use the accessor methods for accessing (return myBottomRight).
Should look something like this: In the constructor: myTopRight = new APPoint( myTopLeft.getX() + myWidth, myTopLeft.getY() ); In the accessor: return myTopRight;
1

You never initialize myTopRight, myBottomLeft, or myBottomRight in your constructor.

Comments

1

You haven't initialized/instantiated some fields. You should always initialize fields before you use them.

Something like this:

public APRectangle( APPoint topLeft, double width, double height )
    {
         myTopLeft=topLeft;
         myTopRight=new APPoint(0,0);
         myBottomLeft=new APPoint(0,0);
         myBottomRight=new APPoint(0,0);
         myWidth=width;
         myHeight=height;
    }

2 Comments

I'm not supposed to initialize myBottomRight because it's supposed to be derived from myTopRight or myBottomLeft <- those are derived from myTopLeft which is the only one im allowed to initialize
not to mention that myBottomRight is not used in getBottomRight().
1

A NullPointerException means that you have a null value being dereferenced. e.g. if you got that exception in the following line of code:

 foo.doSomething();

that would mean that foo == null.

you could:

  1. hook up a debugger and put a breakpoint right before the exception is thrown to inspect the state, or ...
  2. add a few printlns to the relevant bit of code

either one of these should help you figure out which variable is null.

Comments

0

Check myTopRight variable. Your exception may cause this value is null.Debug it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.