Panthy
Panthy

Reputation: 1625

null exception java

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

Upvotes: 0

Views: 1180

Answers (5)

jtahlborn
jtahlborn

Reputation: 53694

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.

Upvotes: 1

Sai Ye Yan Naing Aye
Sai Ye Yan Naing Aye

Reputation: 6738

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

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94653

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

Upvotes: 1

LastStar007
LastStar007

Reputation: 775

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.

Upvotes: 2

jrad
jrad

Reputation: 3190

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

Upvotes: 1

Related Questions