저는이 문제를 많이 조사해 왔으며 해결책을 찾을 수 없습니다. 나는 미니 게임을 만들려고 노력 중이며 플랫폼을 만드는 방법이 있습니다. 모든 플랫폼 매개 변수가있는 클래스가 있고 클래스 배열을 만들었 기 때문에 동시에 여러 플랫폼을 가질 수 있습니다.클래스 배열에서 메서드를 호출하면 NullPointerException이 발생합니다.
문제 : 내가 원하는 매개 변수를 전송하여 플랫폼을 구성하는 방법을 호출하려고하면 NullPointerException
이 표시됩니다. 메서드는 이전에 작동하지만 모든 정적 및 그래서 그 클래스의 여러 인스턴스를 couldnt, 이제 플랫폼 클래스에서 정적 필드를 제거하고 나에게 NullPointerException
메서드를 호출 할 때마다 제공합니다.
public Load_Stage load = new Load_Stage();
public Game() {
-other variables initializatin-
Initialize_Items();
load.Stage_1(); // <--- problem this way
Load_Stage 클래스에서 :
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);
}
}
게임 클래스에서
public static void main(String[] args) {
Game ex = new Game();
new Thread(ex).start();
}
그리고 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