0

コードを実行するたびに、コードから java.lang.NullPointerException: null というエラーが発生します。

コードは次のとおりです。

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

}

最後の方法の前の 3 つの方法は完全にうまく機能しますが、最後の方法が機能しない理由はわかりません。誰か助けてくれませんか?

ありがとう、ローハン

4

5 に答える 5

2

、、、およびを初期myTopRight化するのを忘れました。他の方法は、実際のポイントを使用しないため、正常に機能します(ただし、おそらく使用する必要があります)。APコンピュータサイエンス?myBottomLeftmyBottomRight

ちなみに、例外メッセージは次のようになっています。オブジェクトの1つが存在しないため、問題が発生しています。APRectangle.getBottomRight(59行目)を試したときに問題が見つかりました。MainClass.printBottomRight(35行目)から指示されたため、APRectangle.getBottomRightを実行していました。MainClass.main(46行目)から指示されたため、MainClass.printBottomRightを実行していました。これで問題が解決することを願っています。

于 2012-07-23T03:20:43.547 に答える
1

NullPointerExceptionは、参照解除されているnull値があることを意味します。たとえば、次のコード行でその例外が発生した場合:

 foo.doSomething();

それはそれを意味しfoo == nullます。

あなたは出来る:

  1. デバッガーを接続し、例外がスローされる直前にブレークポイントを設定して状態を検査します。または...
  2. コードの関連ビットにいくつかのprintlnを追加します

これらのいずれかが、どの変数がnullであるかを判断するのに役立ちます。

于 2012-07-23T03:18:08.030 に答える
1

コンストラクターで myTopRight、myBottomLeft、または myBottomRight を初期化することはありません。

于 2012-07-23T03:15:19.770 に答える
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;
    }
于 2012-07-23T03:15:56.000 に答える
0

myTopRight変数を確認してください。例外により、この値がnullになる可能性があります。デバッグしてください。

于 2012-07-23T03:34:48.013 に答える