2014-02-08 64 views
0

我有這樣的構造函數通過參數在我的磁帶類:爪哇 - 與數組實例化一個對象作爲參數

public class Cassette { 

private int cass_Num; 
private String cass_Titre; 
private String cass_Realisat; 
private int nbCopie; 
private int [] cass_Emprunteur; 

//...code 
public Cassette(int num, String titre, String real, int copies, int[] nbEmp){ 
    this.cass_Num = num; 
    this.cass_Titre = titre; 
    this.cass_Realisat = real; 
    this.nbCopie = copies; 
    for(int i=0;i<nbEmp.length;i++){this.cass_Emprunteur[i] = nbEmp[i];} 
} 
//set methods... 
//get methods... 
}//end 

我想要實例在我的主紙盒類的一些對象來測試某些功能爲我的分配。 我不能爲我的生活,找到合適的方式來做到這一點沒有得到一個null.Pointer.Exception

這些都是在我的主要線路:

//...code 
Cassette [] tabCas = new Cassette[MAX_CASSETTES]; 
for(int i=0;i<tabCas.length;i++){tabCas[i]= new Cassette();} 
tabCas[0] = new Cassette(0001,"Jurassic Pork","Steven Swineberg",7,new int[] {11111,44444}); //<--- error here 
//...code 

感謝您的幫助!

+0

查看異常的堆棧跟蹤,看看哪一行正在拋出它?然後看看該行上的變量如何爲空。 – vanza

回答

0

在你Cassette構造函數來訪問,你想:

cass_Emprunteur = nbEmp; 

的替代線路:

for(int i=0;i<nbEmp.length;i++){this.cass_Emprunteur[i] = nbEmp[i];} 

,或者,您需要在使用它的循環之前初始化int[]

+0

謝謝!這與C++有點不同,但我正在學習。 – Tin

1

看看你的構造函數 「this.cass_Emprunteur」 爲空並嘗試與this.cass_Emprunteur [I]

+0

this.cass_Emprunteur需要初始化... –