2016-09-29 6 views
-2

현재 선택 정렬 및 거품 정렬 코드로 어려움이 있습니다. 선택 정렬은 학생 ID를 오름차순으로 정렬하고 버블 정렬은 성을 오름차순으로 정렬하는 데 사용됩니다. 이 프로그램은 선택 10 다음과 같이 내 배열이 선언 11선택 정렬 및 거품 정렬 오류

을 선택시 충돌을 제외한 컴파일 :

student[] list = new student[100]; //my array 

이 내가 선택 정렬 및 거품 정렬을 위해 가지고있는 코드입니다. 메서드가있는 배열을 사용하고 있습니다.

if (choice == 10) { // Dissplay the sorted array by student id 

      SortArrayBySelection(list); 

      System.out.println("Sorted studentid are:"); 
      for (int i =0; i< studentNumber; i++) 
      { 
       System.out.println(list[i]); 
      } 



     } 

     if (choice == 11){ // Display the sorted array by family name 

      BubbleSort(list); 

      System.out.println("The sorted names are:"); 
      for(int i = 0; i < studentNumber; i++) 
      { 
       System.out.println(list[i].Getfamilyname()); 
      } 
     } 


    } while (choice != 1); 



} 

public static void SortArrayBySelection(student[] arrayToSort){ // Function to sort out the array on sutdentid 
for(int i = 0; i < arrayToSort.length-1; ++i) 
{ 
    int minIndex = i; 
    int studentid3 = arrayToSort[i].Getstudentid(); 
    int studentid2 = arrayToSort[minIndex].Getstudentid(); 
    for(int j = i + 1; j <arrayToSort.length; ++j) 
    { 
     int studentid1 = arrayToSort[j].Getstudentid(); 
     if(studentid1 < studentid2) 
     { 
      minIndex = j; 
     } 
    } 
    int temp = studentid3; 
    studentid3 = studentid2; 
    studentid2 = temp; 

} 
} 

public static void BubbleSort(student[] arraySort){ 
    String t; 
    for(int i = 0; i<arraySort.length; i++){ 
     for(int j=0; j<arraySort.length-1;j++){ 
      String str1 = arraySort[j].Getfamilyname(); 
      String str2 = arraySort[j+1].Getfamilyname(); 
      if(str1.compareTo(str2)<0){ 
       t = str1; 
       str1 = str2; 
       str2 = t; 
      } 
     } 
    } 

} 

어떤 의견이라도 고맙겠습니다! 당신에게 오류 감사합니다 : 당신이 당신의 코드에서 line numbers 언급하지 않은 것처럼

Exception in thread "main" java.lang.NullPointerException 
    at client.Client.SortArrayBySelection(Client.java:270) 
    at client.Client.main(Client.java:232) 

Exception in thread "main" java.lang.NullPointerException 
    at client.Client.BubbleSort(Client.java:288) 
    at client.Client.main(Client.java:246) 
+1

일부 예외가 발생했습니다. 어느 것이 어떨까요? – Rufi

+1

당신이 얻고있는 오류는 무엇입니까? '프로그램이 컴파일되지만 10이나 11을 선택하면 충돌합니다.'오류가 발생하지 않습니까? 그 오류 로그를 게시 하시겠습니까? –

+0

오류 : 스레드에서 예외 "주"java.lang.NullPointerException이 client.Client.SortArrayBySelection (Client.java:270) client.Client.main에서 \t (Client.java:232) 예외에에서 \t 스레드 "주"java.lang.NullPointerException \t at client.Client.BubbleSort (Client.java:288) \t at client.Client.main (Client.java:246) – mandok

답변

0

또한 Student class하고 student[] list = new student[100]을 준비하고있는 코드입니다. 그래서, 지금까지 내가 다음 줄의 코드를 볼 수 있습니다 당신은 당신이 그 값을 만드는 것처럼 java.lang.NullPointerException

  • 귀하의 list 길이는 항상 100 될 것입니다 수 있습니다. 따라서 런타임에 준비중인 동적 값이 있다면 ArrayList<student> list=new ArrayList<student>();을 사용하고 list.add(yourObject);을 사용하여 값을 더하는 것이 좋습니다. 당신이 SortArrayBySelection method를 호출이 오류와 위의 오류에 끝낼 수있는 라인을 위해

    • Exception in thread "main" java.lang.NullPointerException at client.Client.SortArrayBySelection(Client.java:270) at client.Client.main(Client.java:232) 오류에 대한
      int studentid3 = arrayToSort[i].Getstudentid(); int studentid2 = arrayToSort[minIndex].Getstudentid(); int studentid1 = arrayToSort[j].Getstudentid();

이유입니다values이 없습니다.그리고 length 100의 배열을 만들 때 10-15 값만 입력하면 다른 위치에는 값이 없습니다. 하지만 for-loop100 index position으로, 10-15 index 이후에는 항상 getters으로 전화하여 null이됩니다.

같은 이유로 다른 방법이 있습니다.