-2

Currently having difficulty with Selection Sort and Bubble Sort codes. The selection sort is used to sort out student ID in ascending order and the bubble sort is used to sort out last names in ascending order. The program compiles but crashes upon choosing choice 10 or 11.

My array is declared as follows:

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

This is the code that I have for selection sort and bubble sort. I am using an array with methods:

   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;
            }
        }
    }

}

Any suggestions would be appreciated! thank you Errors:

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)
mandok
  • 17
  • 1
  • 6
  • 1
    I guess that some exception is thrown, which one? – Rufi Sep 29 '16 at 05:38
  • 1
    what is the error you are getting,`The program compiles but crashes upon choosing choice 10 or 11` it is not throwing any error ? post that error log? – Rishal Sep 29 '16 at 05:38
  • Errors: 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) – mandok Sep 29 '16 at 06:18
  • any suggestions? The error codes are up... – mandok Sep 29 '16 at 07:18
  • Ye your error's show line number and we can't see line numbers in your code, so they are not helping at all, but probably your array is not filled completely. – Shadov Sep 29 '16 at 08:43

1 Answers1

0

As you have not mentioned line numbers in your code, also the Student class and the code where you are preparing the student[] list = new student[100]. So, as far as i can see the code from following lines you can get the java.lang.NullPointerException

  • Your list length will be always 100 as you are creating it with that value. So if it something dynamic values which you are preparing at runtime then it will be better if you use ArrayList<student> list=new ArrayList<student>(); and add the values using list.add(yourObject);.

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

    • For this error you are calling SortArrayBySelection method and the lines which may end up in the above errors are
      int studentid3 = arrayToSort[i].Getstudentid(); int studentid2 = arrayToSort[minIndex].Getstudentid(); int studentid1 = arrayToSort[j].Getstudentid();

Reason for the error : You don't have the values present at the given index and as you are creating an array of length 100 where you put only let say 10-15 values then it will always have no values in other location. But your for-loop will go to 100 index position in which after 10-15 index you will always get null by calling the getters.

Same reason is for your other method as well.

Rishal
  • 1,480
  • 1
  • 11
  • 19