2015-10-22 62 views
-4

我遇到了「java.lang.NullPointerException」的問題。 此代碼的目標是生成隨機數與一個特定大小的數組(在這種情況下,10個數字,在驅動程序中給出,但沒關係),計算平均值,找到最大值,最小值和a具體數字(如驅動程序中給出的,2和3,但沒關係)。我想我可以用一些研究來編寫所有這些代碼,但是在完成所有工作並編譯完成之後,我嘗試運行代碼,但令我驚訝的是,我發現了java.lang.NullPointerException。我不知道它是什麼,但通過我學習的一些研究和(我認爲)我知道有什麼問題。我想使用「for循環」訪問數組,但我不能,因爲數組的值爲空。我不知道爲什麼,也不知道這段代碼中的所有錯誤,因爲我仍然試圖運行它。我只能編譯,但正如我所說,我運行時遇到了NPE問題。然而,對我來說,在我寫的方式中,接受參數的構造函數和「for循環」在邏輯上是可以接受的。 所以,當我運行,該消息是正是這一點:java.lang.NullPointerException數組For循環

顯示java.lang.NullPointerException在 ArrayLab.initialize(ArrayLab.java:19)在Driver.main(Driver.java:16)

我的問題是: 我的代碼有什麼問題?爲什麼我的數組爲null並且沒有在驅動程序中分配的值?我怎樣才能給這個數組賦值?我的構造函數有什麼問題嗎?那麼「for循環」呢?我能做些什麼來解決它?

public class ArrayLab 
{ 
    private int[] ArrayNum; 

    public ArrayLab(int Num) 
    {   
     int[] ArrayNum = new int[Num];   
    } 

    public void initialize() 
    {   
     for (int ANUM = 0; ANUM <= ArrayNum.length; ANUM++) 
     {    
      ArrayNum[ANUM] = (int) Math.round((Math.random() * 10));   
     }   
    }  

    public void print() 
    { 
     System.out.println(ArrayNum); 
    } 

    public void printStats() 
    { 
     double Sum = 0; 
     int Max = 0; 
     int Min = 0; 

     for (int ANUM = 0; ANUM < ArrayNum.length; ANUM++) 
     {    
      Sum = Sum + ArrayNum[ANUM];  

      if (ArrayNum[ANUM] > ArrayNum[Max]) 
      { 
       Max = ANUM; 
      }    

      if (ArrayNum[ANUM] < ArrayNum[Min]) 
      { 
       Min = ANUM; 
      } 
     } 

     double Average = Sum/ArrayNum.length; 
     System.out.println("Average Value: " + Average); 
     System.out.println("Maximum Value: " + Max); 
     System.out.println("Minimum Value: " + Min); 
    }  

    public void search(int GivenNumber)  
    { 
     for (int ANUM = 0; ANUM < ArrayNum.length; ANUM++) 
     {    
      if(GivenNumber == ArrayNum[ANUM]) 
      { 
       System.out.println(GivenNumber + " was found."); 
      } 

      else 
      { 
       System.out.println(GivenNumber + " was not found."); 
      } 
     } 
    } 
} 

public class Driver 
    { 
     public static void main(String[] args) 
     { 
      //create new instance of the ArrayLab class with parameter of 10 
      ArrayLab array = new ArrayLab(10); 

      //print out the array created by ArrayLab constructor 
      array.print(); 

      //initialize the array with random values 
      array.initialize(); 

      //print the randomized array 
      array.print(); 

      //print stats for the array 
      array.printStats(); 

      //search for 3 
      array.search(3); 

      //search for 2 
      array.search(2); 
     } 
    } 
+1

可能重複[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-是一個空指針異常和怎麼辦我修復它) – John3136

+0

調試你的代碼,並看看錯誤信息中指定的行,你有一些對象是空的,但您嘗試從該對象調用某種方法。因此你得到NullPointerException。 – Rufi

+0

在你的構造函數中,你已經聲明瞭你的數組。所以你的實際數組沒有被分配任何內存 – SacJn

回答

0

在你的構造函數,你已重新宣佈的陣列。所以你的實際陣列沒有被分配任何內存

private int[] ArrayNum; 

public ArrayLab(int Num) 
{   
    ArrayNum = new int[Num];   
} 
+0

謝謝SacJn!它爲我工作。我可以編譯並運行它!完善。 – ZessLamperouge