2012-07-23 67 views
0

我从代码中得到一个错误,说java.lang.NullPointerException:null 每当我运行它。null异常java

下面是代码:

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

的最后一个方法是给我的错误之一。

这里是我的主类:

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

} 

最后一个工作之前,这三种方法非常好,但我不知道为什么最后一个不工作。有人能帮助我吗?

感谢,罗汉

+0

你从哪里得到例外? – 2012-07-23 03:13:16

+0

当我编译完所有东西后运行主类时,我发现异常说 java.lang.NullPointerException \t at APRectangle。getBottomRight(APRectangle.java:59) \t在MainClass.printBottomRight(MainClass.java:35) \t在MainClass.main(MainClass.java:46) 我真的不知道这意味着什么 – Panthy 2012-07-23 03:16:31

回答

2

您忘记初始化myTopRight,myBottomLeftmyBottomRight。其他方法正常工作,因为你不使用他们的实际点(但你可能应该)。 AP计算机科学?

顺便说一句,你的异常消息是这样说的: 我们有一个问题,因为我们的一个对象不存在。 当我们尝试使用APRectangle.getBottomRight(第59行)时发现问题。 我们在做APRectangle.getBottomRight,因为MainClass.printBottomRight(第35行)告诉我们。 因为MainClass.main(第46行)告诉我们,所以我们在做MainClass.printBottomRight。 希望这可以解决问题。

+0

是AP CS。所以如果我必须初始化所有这些,那么我是否必须为它编写修饰符方法?我该怎么做? – Panthy 2012-07-23 03:22:37

+0

要消除NullPointerException,您不需要为它们编写修饰符方法。你只需要在构造函数中初始化它们,就像初始化myTopLeft一样。 – jrad 2012-07-23 03:24:51

+0

我解决了我的问题,通过编辑getBottomRight公共APPoint getBottomRight() { 返回新的APPoint(myTopLeft.getX()+ myWidth,myTopLeft.getY() - myHeight); }这样它使用一个已经初始化的值;) – Panthy 2012-07-23 03:26:52

1

你从来没有在构造函数初始化myTopRight,myBottomLeft,或myBottomRight。

1

您尚未初始化/实例化some字段。在使用它们之前,您应该始终初始化fields

事情是这样的:

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

我不应该初始化myBottomRight,因为它应该从myTopRight或myBottomLeft派生< - 这些派生自myTopLeft,它是唯一允许初始化的 – Panthy 2012-07-23 03:19:43

+0

,更不用说getBottomRight()中没有使用myBottomRight。 – jtahlborn 2012-07-23 03:21:10

1

一个NullPointerException异常意味着你有一个空值被取消引用。例如如果你有下面的代码行例外:

foo.doSomething(); 

这将意味着,foo == null

你可以:

  1. 钩了一个调试器将断点设置正确的异常被抛出前检查状态,还是......
  2. 几个printlns添加到代码
  3. 的相关位

其中之一应该可以帮助您找出哪个变量为空。

+0

谢谢,也有帮助 – Panthy 2012-07-23 03:30:01

0

检查myTopRight变量。你的异常可能会导致这个值为null.Debug它。