简体   繁体   中英

Nullpointerexception with java bubble sort

I have been working on a project for way to long now and I am running into a nullpointerexception. I understand that it is when an object is pointing to nothing. I am getting this error while doing a bubble sort in Java. I can't figure out what is causing this exception and thus can't resolve it. The purpose of this code is to sort an array of student ID numbers in a specific order, I've chosen descending order.

 public static void idNumber()
    {
        String[] iD = new String[150];  //array for ID Numbers
        //System.out.println("Original order");
        for(int i = 0; i < nNumStudents; i++)   //add ID numbers to array iD
        {
            iD[i] = srStudents[i].getStudentKey();

            //System.out.println(srStudents[i].getStudentKey());
        }
        //bubble sort
        int k =0;
        int j =0;
        boolean exchange = true;
        String temp;
        temp = new String();
        while ((k < iD.length - 1) && exchange)
        {
            exchange = false;
            k++;
            for(j = 0; j < iD.length - k; j++)
            {
                if(iD[j].compareTo(iD[j + 1]) > 0)
                {
                    temp = iD[j];
                    iD[j] = iD[j + 1];
                    iD[j + 1] = temp;       
                    exchange = true;

                }
            }
        }
        System.out.println(iD);
    }

Exception in thread "main" java.lang.NullPointerException
at java.lang.String.compareTo(String.java:1139)
at StudentRegistrar.idNumber(StudentRegistrar.java:152)
at Sort.main(Sort.java:21)

From a glance at your code, my guess is that it is possible that your array size exceeds the number of students. If this is the case, you are attempting to compare empty slots in the array, which would give a null pointer exception. To fix this, increment to nNumStudents rather than to the full length of the array.

This nullpointer is coming up because all the members of String array String[] iD = new String[150]; are not initialize for example the for loop which is populating this iD array is either not running until 150 or one of its members is initialized with null so

First thing print and check what is the value of nNumStudents it should be 150. Then make sure that every value which is assigned to iD array is a non null value you can do this by modifying your code to print all the values it is assigned to

for(int i = 0; i < nNumStudents; i++)   //add ID numbers to array iD
    {
        iD[i] = srStudents[i].getStudentKey();

        //uncomment the below line and see if it doesn't print null

        System.out.println(srStudents[i].getStudentKey());
    }

if it exceeds 150 then you will get an ArrayIndexoutofbound exception not null pointer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM