NullPointerException:基数排序

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

    }
}

我是 java 的初学者。我已经使这段代码使用基数排序对数组进行排序。当我 运行 带有任何测试数组的代码时,我收到此错误:

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)

我是 java 的初学者,所以如果我犯了一些菜鸟错误,请不要取笑我。 如果你给我一个更短的方法来做这件事也会很有帮助。例如减少功能的数量。

是因为你打电话

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

在第一个循环之后。

lsdArray是一个几乎全是空值的数组(因为你没有在Integer[][] lsdArray = new Integer[10][15];中初始化它们)。

因此,当您使用 array[i] = null 传入 int term = (array[i] % powOfTen ); 时,您会得到一个 NPE。

顺便说一下,您的 getMaxDigits 也有问题,它没有返回数组的最大长度元素。

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

它返回数组最后一个元素的长度。相反,你应该做这样的事情

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

如果你想优化你的功能,你也可以删除 var m 并且只使用 max :)