2014-03-25 3 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

당신은 그 곳에 온 정적이라는 단어가 무엇을 의미하는지 이해하지 못하는 것 같습니다. 초기화하지 않은 정적 배열을 참조하는 정적 메서드를 호출하고 있습니다. –

+0

pls stacktrace 붙여 넣기 ... – jmail

답변

1
  • 모든 정적이다. 즉, 작성한 생성자는 호출되지 않으며 배열은 기본값 인 null에서 변경된 적이 없습니다. 생성자 코드를 정적 초기화 블록으로 변경하는 것이 좋습니다.
  • generator은 아무것도로 설정, 그래서 너무 널 그리고 당신은 배열 대신 당신이 insertion(numInt); 호출하면, 방법 public static void insertion(int n)가 호출 arrayMenu[i]
0

arrayMenu[n]를 설정하는 것입니다 초기화 그것을

  • nextInt를 호출 할 수 없습니다 않습니다 그런 다음 for 루프를 수행하려고합니다. for(int i = 1; i < arrayMenu.length; i++)

    그러나 arrayMenu은 초기화되지 않았으므로 null입니다. null에서 길이를 호출하려고하면 NullPointerException이 발생합니다.

  • 0

    당신은 정적 생성자를 추가하고 정적 INT

    //set parameter = n 
    public static int parameter; 
    
    static 
    { 
        arrayMenu = new int[parameter]; 
    } 
    
    관련 문제