2013-12-13 30 views
0

我已經用Java編寫了一個模擬程序,但運行時遇到了一些問題。我爲模擬模型的各個方面而創建的自定義類似乎從未初始化過。例如,爲什麼我創建的對象處於null狀態?

public class Queue { 
public static int front = -1; 
public static int length = 0; 
public static int capacity = 0; 
public static int roadqueue[]; 
public static int position[]; 
public static boolean full = false; 

public static void push(int pushValue) { 

然後可以從類運行的功能。

在我的主要項目中,我聲明類如下:

public class myProject { 

public static Queue queue[] = new Queue[33]; 

,然後在主函數中,我調用了應該在這個類初始化值功能的第一件事:

public static void main(String[] args) { 
initQueue(); 

public static void initQueue() { 
queue[0].length = 6000; 
queue[14].length = 6000; 
queue[1].length = 20; 
queue[13].length = 20; 
queue[3].length = 10; 
queue[11].length = 10; 
queue[4].length = 360; 
queue[12].length = 360; 
queue[5].length = 260; 
queue[10].length = 260; 
queue[6].length = 20; 
queue[9].length = 400; 
queue[15].length = 460; 
queue[22].length = 460; 
queue[16].length = 260; 
queue[21].length = 260; 
queue[17].length = 100; 
queue[26].length = 100; 
queue[18].length = 160; 
queue[20].length = 140; 
queue[23].length = 240; 
queue[25].length = 140; 
queue[27].length = 80; 
queue[32].length = 80; 
queue[28].length = 480; 
queue[31].length = 140; 

for (int i = 0; i < 33; i++) { 
    if (i == 3 || i == 12) 
    queue[i].capacity = 40; 
    else 
    queue[i].capacity = queue[i].length/10; 
    queue[i].position = new int[queue[i].capacity]; 
    queue[i].roadqueue = new int[queue[i].capacity]; 
} 
} 

然而,當我運行該項目,並插入斷點時, for循環迭代開始,但Netbeans的通知我,例如在

queue[0].length = 6000; 

「長度引用空對象」,並在可變Explorer在隊列[]中的所有值在整個程序中保持零。任何想法我做錯了什麼?

+0

請出示一個*短* *,但完整的*程序演示該問題。而不是所有單獨的片段,只顯示一個完整的程序 - 它只需要大約10行... –

+2

您需要在陣列的每個插槽中創建一個隊列對象。 I.e'queue [0] = new Queue();隊列[0] .length = 6000;'等 –

+3

爲什麼變量是靜態的?這樣,每個對象都具有相同的值。我不認爲這是正確的輸出 –

回答

2

這條線:

public static Queue queue[] = new Queue[33]; 

...只是創建陣列。你仍然需要在陣列中創建,如:

queue[0] = new Queue(); 

這種區別使得特定的意義,如果你想在你爲什麼擺在首位的陣列而言:每個對象在它可能有不同的狀態。因此,使用一個抽象的例子:

// Create a Person array 
Person[] people = new Person[3]; 

// Add Joe, Mohammed, and Maria to it 
people[0] = new Person("Joe"); 
people[1] = new Person("Mohammed"); 
people[2] = new Person("Maria"); 

(我用明確的指標有,但通常你會遍歷並使用循環變量。)

但需要注意的非常重要的事情喬恩斯基特在評論中提到:您的Queue班級的成員全都是static,因此整個班級只有一個一個length(例如)。正因爲如此,而且由於Java的,此行的一個怪癖:

queue[0].length = 6000; 

實際上做到這一點:

Queue.length = 6000; 

你想使這些成員的實例成員(從他們身上取出static) ,在構造函數中將它們初始化爲相關值,並初始化數組中的條目(上面的queue[x] = new Queue();)。

+0

啊,萬分感謝。我會試試這個,但聽起來它會起作用。 – Martin

+0

@Martin:參見Jon Skeet的評論,你的'Queue'類的成員都是'static'。使它們成爲實例成員並初始化實例會更有意義(根據上述)。 –

4

數組的所有元素都會自動初始化爲默認值(在您的情況下,null)。您有33個空值的陣列Queue s。

正如其他人的建議,你需要使用它們

queue[i] = new Queue(); 
//now you can use queue[i] 

LE之前分配的Queue到元素實例:順便說一下,我懷疑你想在Queue這些領域是static。您正在創建一個Queue的數組,它們都具有相同的字段。您可能需要從這些字段中刪除static關鍵字(這可能還包括從Queue某些方法中刪除static)。

+0

不確定這是否有幫助,因爲'Queue'中的所有變量都是'static' ...無論如何... –

+2

@BoristheSpider但無論如何,數組元素都是'null'。 – async

+2

@ user16547:是的,它們是空的 - 但不會導致NullPointerException,因爲它們沒有在提供的代碼中被取消引用。你可以自己嘗試。 –

1

當您在Java中創建數組時,它會分配數組本身 - 但Array內的所有內容都爲null。本質上,你創建了一個有很多房屋的地塊的街道,但不建造任何房屋。

你需要在每個地塊都經過建築房屋,然後才能對房屋做任何事情。

因此循環遍歷隊列,創建Queue對象。

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

Queue queue[] = new Queue[33];你的陣列是Queue類型,它是一個Object。你初始化你的數組的那一刻。所有元素將分配給它的默認值,在這種情況下,它是null,因爲Object的默認值是null

0

我想你想不到,只有你有隊列對象。你需要在陣列的每個位置都有一個。

queue[1] = new Queue(); 
相關問題