javaradix-sort

NullPointerException: radixSort


import java.lang.Math;




public class radixSort{
    public static int getFreeSpace(Integer array[]){        //this function gets the first occurence of a null element in the array
        int i;
        for (i = 0; i < array.length;i++){
            if (array[i] == null){
                break;
            }
        }
        return i;
    }
    public static Integer[] flatten(Integer array[][]){    //this function flattens the multi-dimensional lsdArray to a single dimension array
        Integer finalArray[] = new Integer[1000];
        for (int i = 0; i < array.length; i++){
            Integer[] currentArray = array[i];
            for (int j = 0; j < currentArray.length; j++){
                if (array[i][j] != null){
                    int index = getFreeSpace(finalArray); 
                    finalArray[index] = currentArray[j];
                                        
                }
                else;     
                                         
            }

            
            
        }
        return finalArray;
    }
    public static Integer getMaxDigits(Integer array[]){    //this function gets the largest number of digits in the array. this is helplful for getting the number of times the array is passed into the sort
        Integer max = 0;
        Integer m = 0;
        for (int i = 0; i < array.length; i++){
            m = Math.max(max, array[i]);
        }
        String s = m.toString();
        return s.length();
    }

    public static Integer[] radixSortFunc(Integer array[],Integer maxDigits, Integer counter){ //the sorting algorithm
        Integer[][] lsdArray = new Integer[10][15];
        if (counter <= maxDigits){
            
            for (Integer i = 0; i < array.length; i++){
                double temp = Math.pow(10,counter+1);
                int powOfTen = (int) temp;
                int term = (array[i] % powOfTen );
                temp = Math.pow(10,counter);
                powOfTen = (int) temp;
                int digit = Math.floorDiv(term,powOfTen);
                                  
                Integer[] currentArray = lsdArray[digit];
                Integer index = getFreeSpace(currentArray);
                currentArray[index] = array[i];
                lsdArray[digit] = currentArray;


                
    
                }
        
            counter++;           
            return radixSortFunc(flatten(lsdArray),maxDigits,counter);
        }
        else{
            return array;
        }
        

    }

    public static void main(String[] args){
        Integer[] array ={12,23,45,23,166};
        Integer max = getMaxDigits(array);
        Integer[] sortedArray = radixSortFunc(array,max,0);
        for (Integer i = 0; i < sortedArray.length; i++){
            if (sortedArray[i] != null){
                System.out.println(sortedArray[i]);
            }
            else;
            
        }
        

    }
}

i am a begginer to java. i have made this code to sort an array using radix sort. when i run the code with any test array i get this error:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "array[java.lang.Integer.intValue()]" is null
        at radixSort.radixSortFunc(radixSort.java:52)
        at radixSort.radixSortFunc(radixSort.java:68)
        at radixSort.main(radixSort.java:80)

i am a total begginer in java, so please dont tease me if i have made some noob mistake. it would also be helpful if you give me a shorter way to do this. for example lowering the number of functions.


Solution

  • It's because you call

    return radixSortFunc(flatten(lsdArray),maxDigits,counter);

    after the first loop.

    lsdArray is an array almost full of null value (because you didn't initialize them in Integer[][] lsdArray = new Integer[10][15];).

    So, when you pass in int term = (array[i] % powOfTen ); with array[i] = null you get a NPE.

    By the way, you also have an issue with your getMaxDigits, it's not returning the maxlength element of your array.

        for (int i = 0; i < array.length; i++){
            m = Math.max(max, array[i]);
        }
    

    It's returning the length of the last element of your array. Instead you should do something like this

        for (int i = 0; i < array.length; i++){
            max = Math.max(max, array[i]);
            m = max;
        }
    

    You might also remove var m and only use max if you want to optimize your function :)