2014-03-25 96 views
-1

我是編程新手,不明白爲什麼當我嘗試初始化n,numInt和arrayMenu時,程序給我一個NullPointerException的運行時錯誤。這似乎沒有任何工作。該程序的工作是收集一組隨機整數存儲在一個數組中,並允許用戶選擇從哪種類型中進行選擇。謝謝閱讀。Java爲什麼在實例化我的數組時遇到NullPointerException

import java.util.Scanner; 
import java.util.Random; 
public class VariousSortsHS 
{ 
    private static int[] arrayMenu; 
    private static Random generator; 

    /** 
    * Constructor for objects of class VariousSortsHS. 
    */ 
    public VariousSortsHS(int n) //The error starts here 
    {         
     arrayMenu = new int[n]; //I don't get why it says null in the array when 
           //i am initializing the length of the array to n 
     /*Assigns a random number between 0 too 100.*/ 
     for(int i = 0; i < n; i++) 
     { 
      int temp = generator.nextInt(100); 
      arrayMenu[n] = temp; 
     }  
    } 

    /** 
    * Selection Sort method. 
    */ 
    public static void selection(int n) 
    { 
     for(int i = 0; i < arrayMenu.length - 1; i++) 
     { 
      int minPos = i; 
      for(int j = i + 1; j < arrayMenu.length; j++) 
      { 
       if(arrayMenu[j] < arrayMenu[minPos]) minPos = j; 
      } 
      int temp = arrayMenu[i]; 
      arrayMenu[i] = arrayMenu[minPos]; 
      arrayMenu[minPos] = temp; 
      System.out.print(temp + " "); 
     } 
    } 

    /** 
    * Insertion Sort method. 
    */ 
    public static void insertion(int n) 
    { 
     for(int i = 1; i < arrayMenu.length; i++) 
     { 
      int next = arrayMenu[i]; 
      int j = i; 
      while(j > 0 && arrayMenu[j - 1] > next) 
      { 
       arrayMenu[j] = arrayMenu[j - 1]; 
       j--; 
      } 
      arrayMenu[j] = next; 
      System.out.print(next + " "); 
     } 
    } 

    /** 
    * Quick Sort method. 
    */ 
    public static void quick(int n) 
    { 
     int pivot = arrayMenu[0]; 
     int i = 0 - 1; 
     int j = n + 1; 
     while(i < j) 
     { 
      i++; while(arrayMenu[i] < pivot) i++; 
      j++; while(arrayMenu[j] > pivot) j++; 
      if(i < j) 
      { 
       int temp = arrayMenu[i]; 
       arrayMenu[i] = arrayMenu[j]; 
       arrayMenu[j] = temp; 
       System.out.print(temp + " "); 
      } 
     } 
    } 

    /** 
    * Main method that allows user to input data. 
    */ 
    public static void main(String[] args) 
    { 
     Scanner in = new Scanner(System.in); 
     System.out.println("Do you wish to sort random integers? (Yes or No) "); 
     String answer = in.next(); 
     String answer2 = answer.toLowerCase(); 

     do 
     { 
      /*Prompts for array length.*/ 
      System.out.println("How many random integers do you wish to sort?"); 
      int numInt = in.nextInt(); 
      /*Promps for sort selection choice.*/ 
      System.out.println("Select a sort to use: \n\t1)Selection\n\t2)Insertion\n\t3)Quick"); 
      String sort = in.next(); 
      String sort2 = sort.toLowerCase(); 
      if(sort2.equals("selection")) 
      { 
       selection(numInt); 
      } 
      else if(sort2.equals("insertion")) 
      { 
       insertion(numInt); 
      } 
      else if(sort2.equals("quick")) 
      { 
       quick(numInt); 
      } 
      else 
      { 
       System.out.println("You have entered the wrong input."); 
      } 
     } while(!answer2.equals("no")); 
    } 
} 
+0

'generator'可能是評估爲null初始化大小。使用調試器和/或更好地讀取跟蹤。 – user2864740

+0

你似乎無法理解你在這個地方所說的「static」這個詞的意思。您正在調用引用靜態數組的靜態方法,這是您從未初始化過的。 –

+0

請粘貼stacktrace ... – jmail

回答

1
  • 一切都在你的代碼是靜態的。這意味着您編寫的構造函數不會被調用,並且該數組從未從其默認值null更改過。考慮將您的構造函數代碼改爲靜態初始化塊。
  • generator是從來沒有設置任何東西,所以它的空太,你不能在它
  • 調用nextInt初始化數組設置arrayMenu[n]代替arrayMenu[i]
0

當你調用insertion(numInt);,方法public static void insertion(int n)被稱爲然後你試圖做這樣的for循環for(int i = 1; i < arrayMenu.length; i++)

但是,arrayMenu未初始化,它爲空。當您嘗試調用一個空值時,您將得到NullPointerException。

0

您需要添加靜態構造函數和使用靜態INT

//set parameter = n 
public static int parameter; 

static 
{ 
    arrayMenu = new int[parameter]; 
} 
相關問題