私はこの問題を多く探していますが、解決策が見つかりません。私はミニゲームを構築しようとしており、プラットフォームを作成する方法があります。私はすべてのプラットフォームパラメータを持つクラスを持っているので、私はクラスの配列を作ったので、同時に複数のプラットフォームを持つことができます。クラス配列からメソッドを呼び出すとNullPointerExceptionが発生します
問題:私が望むパラメータを送信してプラットフォームを構築する方法を呼び出そうとすると、私にNullPointerException
が与えられます。メソッドは以前は動作していましたが、すべて静的なので、そのクラスの複数のインスタンスを持つことができませんでしたが、今ではプラットフォームクラスから静的フィールドを削除して、メソッドを呼び出すたびにNullPointerException
を返します。ゲームのクラスで
public static void main(String[] args) {
Game ex = new Game();
new Thread(ex).start();
}
:Load_Stageクラスで
public Load_Stage load = new Load_Stage();
public Game() {
-other variables initializatin-
Initialize_Items();
load.Stage_1(); // <--- problem this way
:
私は私にエラーを与えるコードの一部をコピーし、エラーは次のようになります
public class Load_Stage {
public Platforms plat = new Platforms();
public void Stage_1(){
Stage_Builder.Build_Platform(200, 500, 300, plat.platform1);
Stage_Builder.Build_Platform(100, 200, 100, plat.platform1);
}
}
Stage_Builderクラスの内部:
public class Stage_Builder {
public static final int max_platforms = 10;
public static Platform_1[] p1 = new Platform_1[max_platforms];
public static boolean[] platform_on = new boolean[max_platforms];
public Stage_Builder() {
for (int c = 0; c < platform_on.length; c++) {
platform_on[c] = false;
}
}
public static void Build_Platform(int x, int y, int width, ImageIcon[] type) { // BUILDS A PLATFORM
for (int b = 0; b < max_platforms; b++) {
if (platform_on[b] == false) {
p1[b].Construct(x, y, width, type); // <-- NullPointerException here
platform_on[b] = true;
break;
}
}
}
}
ありがとうございます。
編集:ここではPlatform_1クラスは(それを忘れて申し訳ありません)です:
public class Platform_1 {
private int platform_begin_width = 30;
private int platform_middle_width = 20;
public int blocks_number = 0;
public ImageIcon[] platform_floors = new ImageIcon[500];
private int current_width = 0;
public int [] platform_x = new int [500];
public int platform_y = 0;
public int platform_width = 0;
public void Construct(int x, int y, int width, ImageIcon [] type) {
platform_width = width;
platform_y = y;
for (int c = 0; current_width <= platform_width; c++) {
if (c == 0) {
platform_x[c] = x;
platform_floors[c] = type[0];
current_width += platform_begin_width;
} else if ((current_width + platform_middle_width) > platform_width) {
platform_floors[c] = type[2];
blocks_number = c + 1;
platform_x[c] = current_width + x;
current_width += platform_middle_width;
} else {
platform_floors[c] = type[1];
platform_x[c] = current_width + x;
current_width += platform_middle_width;
}
}
}
}
やプラットフォームクラス:
public class Platforms {
public ImageIcon[] platform1 = {new ImageIcon("Resources/Sprites/Stage_Objects/Platform1/begin.png"),
new ImageIcon("Resources/Sprites/Stage_Objects/Platform1/middle.png"),
new ImageIcon("Resources/Sprites/Stage_Objects/Platform1/end.png")};
}
p1配列コードを忘れて申し訳ありません。私はその記事を編集した。私はあなたのソリューションをチェックし、それが動作するかどうかを確認します:) –
さて、どのようにp1がnullでないインスタンスのPlatform_1を指すように初期化できますか?私はあなたが意味することを知っている、私はちょうど解決策を実装する方法がわからない –
例のための追加されたコードを参照してください – duffymo