2012-01-26 5 views
0

저는이 문제를 많이 조사해 왔으며 해결책을 찾을 수 없습니다. 나는 미니 게임을 만들려고 노력 중이며 플랫폼을 만드는 방법이 있습니다. 모든 플랫폼 매개 변수가있는 클래스가 있고 클래스 배열을 만들었 기 때문에 동시에 여러 플랫폼을 가질 수 있습니다.클래스 배열에서 메서드를 호출하면 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")}; 
} 

답변

3

문제와 해결책은 모두 명백합니다. 코드 행이 실행되면

public static Platform_1[] p1 = new Platform_1[max_platforms]; 

는, P1은 모두 널있는 유형의 참조 Platform_1 의 배열이다.이 코드 줄을 실행

그래서 바로 알려줍니다 :

  p1[b].Construct(x, y, width, type); // <-- NullPointerException here 

이 솔루션은 Platform_1의 널이 아닌 인스턴스를 가리 키도록 p1 배열을 초기화하기입니다. 이 같은

뭔가 작동합니다 : 당신이 채워지지 않았다에 메시지를 호출

for (int i = 0; < p1.length; ++i) { 
    p1[i] = new Platform1(); 
} 
+0

죄송합니다 p1 배열 코드에 대해 잊어 버려. 나는 그 지위를 편집했다. 귀하의 솔루션을 확인하고 작동하는지, 감사합니다 :) –

+0

좋아, 어떻게 p1이 아닌 null 인스턴스를 가리 키도록 초기화 할 수 Platform_1?내가 무슨 뜻인지, 난 단지 솔루션을 구현하는 방법을 잘 모르겠어요 –

+0

예를 들어, 추가 코드보기 – duffymo

2

당신이 p1 배열에 물건을 넣어 어디 보이지 않아요 Stage_Builder 클래스에 있습니다.

다른 가능성 (드물지만 모든 것을 표시하지 않은 경우 가능)은 표시하지 않은 Platform 클래스의 내용이 초기화되지 않고 Construct에 전화하면 깨지기 쉽습니다.

또한, 다음은 당신이 정적 변수를 선언 나타납니다 p1platform_on,하지만 당신은 단지 생성자에서 platform_on을 채울

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

문제가있는 것 같다. 그래서 당신이 Stage_Builder 인스턴스를 생성 처음으로, 당신은

정적 블록에 그 정적 변수를 채 웁니다 ... 모든 false 하나 개의 정적 배열을 채우고, 다른 정적 배열에 아무것도 넣지 마십시오

// static var declarations 

static { 
    // populate static arrays here. 
} 
+0

p1 어레이 코드를 잊어 버려 죄송합니다. 나는 그 지위를 편집했다. 난 당신의 솔루션을 확인하고 작동하는지, 고마워 :) –

+0

당신은'p1' 배열을 채우는거야? – hvgotcodes

+0

나는 실제로 ... 그것을 채우지 않고있다, 나는 그것을 지금 할 것이다, 그것이 효과가 있는지 보아라. 전에도 같은 논리를 사용했기 때문에 문제가 아니라고 생각했습니다.하지만 Kosta가 제안한대로 ArrayList를 사용할 것입니다. 작동하는지 확인하십시오. 감사. –

0

배열입니다.

당신은 당신은

null.Construct(...); 
입니다

p1[b].Construct(x, y, width, type); 

호출 할

public static Platform_1[] p1 = new Platform_1[max_platforms]; 

그렇게 p1

p1[0] = null 
p1[1] = null 
. 
. 
. 
p1[max_platforms] = null 

있다가

배열의 인덱스를 먼저 초기화해야합니다.

p1[b] = new Platform_1(); 
p1[b].Construct(...); 
+0

고마워, 그게 문제 인 것 같다. 나는 지금 그것에 대해 연구 할 것이다 : D –

0

먼저 duffymo가 지적한대로 p1 [b]가 null 일 가능성이 높습니다.

둘째, 정말 이상한 방식으로 배열을 사용하고 있습니다. 아니오)

p1.add(new Platform1(x, y, width, type); 

D :

에 대해

A) 대신에) Stage_Builder

B를 삭제 그렇다면 ArrayList에 어딘가에

C) Build_Platform1의 등가()이 모습을 가지고 on [i], no max_platforms, for 루프가 플랫폼을 추가하지 않음 (후자는 실제로 몇 hundret 플랫폼을 사용하는 경우 나쁜 성능 문제입니다)

+0

와우, 좋은 개선 같아. 고마워, 난 전에 ArrayLists를 다루지 않았지만 당신이 제안한 것을 잘못했다. 문제에 관해서는, 나는 p1 (Platform_1) 클래스 코드로 게시물을 편집했습니다. 내가 아는 한, 모든 것이 올바르게 초기화되어 있습니다. 고마워 –

관련 문제