2012-01-26 32 views
0

我一直在爲這個問題尋找很多,我找不到解決方案。我試圖建立一個迷你遊戲,我有一個創建平臺的方法。我有一個每個平臺參數的類,我做了一個類的數組,所以我可以在同一時間有多個平臺。從類數組中調用方法會導致NullPointerException

問題:當我嘗試通過發送參數來調用構建平臺的方法時,它給了我一個NullPointerException。該方法之前工作,但一切都是靜態的,所以我不能擁有該類的多個實例,現在我從平臺類中刪除了靜態字段,並且每次調用該方法時都會給我提供NullPointerException

我複製了給我的錯誤代碼的一部分,錯誤進入方式如下:

public static void main(String[] args) { 
     Game ex = new Game(); 
     new Thread(ex).start(); 
    } 

在遊戲類:

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

} 

而在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 

的解決方案是並初始化的p1陣列指向的Platform_1非空的情況。

像這樣的工作:

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

對不起忘記p1數組代碼。我編輯了這篇文章。我會檢查你的解決方案,看看它是否工作,謝謝:) –

+0

好吧,我如何初始化P1,指向Platform_1的非null實例?我知道你的意思,我只是不知道如何實施解決方案 –

+0

查看示例代碼 – duffymo

2

我沒有看到你把東西p1陣列在Stage_Builder類。

另一種可能性(不太可能,但如果您未顯示所有內容,則可能)是Platform類中未顯示的內容未初始化,當您撥打Construct時該內容已中斷。

此外,下面的問題似乎

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

看來聲明靜態變量p1platform_on,但你只能在構造函數填充platform_on。所以你創建一個Stage_Builder實例中的第一次,你填充一個靜態數組與所有false,並且不要把任何其他靜態數組中......

填充這些靜態變量在靜態塊

// static var declarations 

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

對不起,忘記了p1數組代碼。我編輯了這篇文章。我會檢查你的解決方案,看看它是否工作,謝謝:) –

+0

你在哪裏填充'p1'數組? – hvgotcodes

+0

我實際上......沒有填充它,我現在就做,看看是否有效。我認爲這不是問題,因爲我做了像以前一樣的東西,我用相同的邏輯做了它......但我想我會使用一個ArrayList作爲Kosta建議,看看是否有用。謝謝。 –

0

數組你正在呼籲從未填充的消息。

你有

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

所以p1

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

你嘗試調用

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

這是

null.Construct(...); 

您需要首先在數組上初始化該索引。

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

謝謝,這似乎是問題所在。我現在正在研究它:D –

0

首先,您的問題是,正如duffymo指出的,p1 [b]很可能爲空。其次,你正在以一種奇怪的方式使用數組。什麼

一)刪除Stage_Builder

B)相反,有一個ArrayList地方

C)Build_Platform1的等效()這個樣子,那麼:

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

d)無如果在[i]上沒有max_platforms,則不需要for循環來添加平臺(如果您實際上有幾個hundret平臺,後者是一個糟糕的性能問題)

+0

哇,好像是一個很好的改進。謝謝,我其實並沒有處理過ArrayLists,但生病了你的建議。至於這個問題,我使用p1(Platform_1)類代碼編輯了帖子。據我所知,一切都在那裏正確初始化。感謝:D –

相關問題