2

パッケージから画像を取得するオブジェクトを作成し、後で画面に描画します。コードを netbeans で実行すると、正常に動作します。netbeans の外では、ヌル ポインター例外エラーが発生します。これが私のコードです。println 部分を使用して、「frog」が null に等しいかどうかを確認しました。実行すると、「/images/upFrogStill.png」と等しいと表示されるため、null ポインター例外が発生する理由がわかりません。エラーは「ImageIcon ii...」行にあります。

public class Frog extends Sprite implements Commons {

String frog = "/images/upFrogStill.png";

 public Frog() {
 System.out.println("frog = " + frog);
        ImageIcon ii = new ImageIcon(this.getClass().getResource(frog));
        image = ii.getImage();
        width = image.getWidth(null);
        height = image.getHeight(null);
        resetState();

}
  void resetState() {
     if(frog != null){
     frog = "/images/upFrogStill.png";
    x = 185;
    y = 397;
}}
}
4

1 に答える 1

4

その可能性は高い

ImageIcon ii = new ImageIcon(this.getClass().getResource(frog));リソースが見つからないため、 null返しています。

netbeans の外部で実行している場合は、必ずクラスパスにファイルを含めてください

ClassLoader.getResource is "absolute";

Class.getResource は、プレフィックスとして「/」を付けない限り、クラスのパッケージに対して相対的です。

お役に立てれば。

于 2013-08-26T20:37:44.950 に答える