2011-10-10 34 views
-1

我對這個論壇的東西很新。我總是很擔心在論壇上發帖,但是經過兩個星期的抨擊,我的頭對着我的代碼試圖讓它工作,我終於讓步了。下面的代碼是一些較大代碼的關鍵部分,但我認爲,有些人指出了問題部分。我有一個對象的二維數組,它應該在創建時默認初始化。但是,這似乎並沒有發生,值仍然爲空。這可能是Java不能通過引用傳遞的結果嗎?如果是這樣,那麼爲什麼原始的二維數組實例化完美呢?預先感謝你,拉斯。Java中的空指針錯誤?

class ObjectThing 
    { 
     int someInt; 
     double someDouble; 

     public ObjectThing() 
     { 
      someInt = 0; 
      someDouble = 0; 
     } 

     synchronized private void someIntIncrement() 
     { 
      someInt++; 
     } 

     synchronized public void addSomeDouble (double sd) 
     { 
      someDouble += sd; 
      someIntIncrement(); 
     } 

     synchronized public String toString() 
     { 
      return someInt + "," + someDouble; 
     } 
    } 


    class AnotherObject 
    { 
      String name; 
      ObjectThing [][] someObjectMAtrix; 
      double [][] someDoubleMAtrix01; 
      double [][] someDoubleMAtrix02; 

      public AnotherObject() 
      { 
      } 

      private void initDouble (double [][] mat) 
      { 
      for (double [] i: mat) 
       for (double j: i) 
        j = 0; 
      } 

      private void initObject (ObjectThing [][] so) 
      { 
       for (ObjectThing [] i: so) 
       for (ObjectThing j: i) 
        j = new ObjectThing(); 
      }   

      public void init (int r, int c) 
      { 
       someObjectMAtrix = new ObjectThing [r][c]; 
       someDoubleMAtrix01 = new double [r][c]; 
       someDoubleMAtrix01 = new double [r][c];   
      initObject (someObjectMAtrix); 
       initDouble (someDoubleMAtrix01); 
       initDouble (someDoubleMAtrix02); 
      } 
    } 

    class Driver 
    { 
     public static void main (String [] args) 
     { 
      initializeMethod(); 
     } 

     public void initializeMethod() 
     { 
      AnotherObject [] anotherObjectArray = new AnotherObject [1]; 
      for (AnotherObject i: anotherObjectArray) 
      { 
       i.init(72,72); 
      } 
     } 
    } 
+1

如果您有像標題所示的NullPointerException,那麼將它包含在您的問題中也很有用。 – wolfcastle

+1

「這可能是Java不能通過引用傳遞的結果嗎?」不,你做錯了。也可以提供一個具有可重現錯誤的較小樣本? – FailedDev

+1

你能提供堆棧跟蹤嗎? – therin

回答

0

我相信這是設計。原語將被分配到堆棧上,並且默認情況下總是有某種類型的值。一個對象被分配到堆上,並將通過引用傳遞。在初始化之前,這些引用默認不會指向任何內容。

0

您剛剛初始化AnotherObject對象的數組......但在數組的第一個字段中沒有AnotherObject ...因爲您沒有初始化它,所以它是空的。 在initializeMethod你必須做到以下幾點:

AnotherObject [] anotherObjectArray = new AnotherObject [1]; 
for (AnotherObject i: anotherObjectArray) 
    { 
     i = new AnotherObject(); 
     i.init(72,72); 
    } 

喝彩!

2

的問題是在你的initializeMethod:

 AnotherObject [] anotherObjectArray = new AnotherObject [1]; 
     for (AnotherObject i: anotherObjectArray) 
     { 
      i.init(72,72); 
     } 

Object[] myArray = new Object[1]只爲數組分配空間。它實際上並不創建這些對象。你的代碼塊應該是

 AnotherObject [] anotherObjectArray = new AnotherObject [1]; 
     anotherObjectArray[0] = new AnotherObject(); 
     for (AnotherObject i: anotherObjectArray) 
     { 
      i.init(72,72); 
     } 

這有點笨重。我假設你有一些需要將AnotherObject放入數組中,而你只是沒有顯示它。如果您只需要其中一個對象,那麼只需直接創建它即可。你應該看看Lists

0

Java編譯器將強制你初始化變量,原語或引用類型(比如對象),否則它會產生編譯器錯誤並失敗。

但是,數組初始化將創建一個所需大小的數組,每個元素都設置爲默認值。對於原語,這類似於數字類型0,布爾值爲false。對於參考類型,默認值爲null。這是你問題的根源。

您正確地初始化該數組,但不要使用有效值填充它。所以,它不是通過引用傳遞,而是通過適當的數組初始化,並且知道新初始化數組的默認值。

具體而言,您正在使用for-each構造來遍歷數組。該構造對集合/數組中的每個對象執行操作。

因此,此代碼:

AnotherObject [] anotherObjectArray = new AnotherObject [1]; 
for (AnotherObject i: anotherObjectArray){ 
    i.init(72,72); 
} 

相當於此代碼:

AnotherObject [] anotherObjectArray = new AnotherObject [1]; 
for(int count=0; count < anotherObjectArray ; count++){ 
    AnotherObject tempObj = anotherObjectArray[i]; // This is null, because anotherObjectArray[i] hasn't been initialised yet 
    tempObj.init(72,72); // This is where we get the NullPointerException :(
} 

你能看到這個問題?您正試圖在空執行操作,因此NullPointerException

我相信你真正意圖是這樣的:

AnotherObject [] anotherObjectArray = new AnotherObject [1]; 
for(int count=0; count < anotherObjectArray ; count++){ 
    AnotherObject tempObj = new AnotherObject(); // Here we create the new object 
    tempObj.init(72,72); // Initialise the newly created object 
    anotherObjectArray[i] = tempObj; // Now we store in the array :) 
} 

我希望我解釋這顯然不夠,但如果我的避風港」請要求澄清t :)

+0

問題解決了:)謝謝 – Lasz

+0

如果我的回答有幫助,接受/提升它是表達您欣賞的好方法:) – chrisbunney