Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java气泡排序的Nullpointerexception_Java_Sorting_Nullpointerexception - Fatal编程技术网

使用java气泡排序的Nullpointerexception

使用java气泡排序的Nullpointerexception,java,sorting,nullpointerexception,Java,Sorting,Nullpointerexception,我已经在一个项目上工作了很长时间,现在我遇到了一个nullpointerexception。我知道这是当一个物体没有指向任何东西的时候。我在Java中执行冒泡排序时遇到此错误。我无法找出导致此异常的原因,因此无法解决它。这段代码的目的是按照特定的顺序对学生ID号数组进行排序,我选择了降序 public static void idNumber() { String[] iD = new String[150]; //array for ID Numbers

我已经在一个项目上工作了很长时间,现在我遇到了一个nullpointerexception。我知道这是当一个物体没有指向任何东西的时候。我在Java中执行冒泡排序时遇到此错误。我无法找出导致此异常的原因,因此无法解决它。这段代码的目的是按照特定的顺序对学生ID号数组进行排序,我选择了降序

 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)
publicstaticvoidnumber()
{
String[]iD=新字符串[150];//iD号数组
//系统输出打印项次(“原始订单”);
for(int i=0;i0)
{
温度=iD[j];
iD[j]=iD[j+1];
iD[j+1]=温度;
交换=真;
}
}
}
系统输出打印项次(iD);
}
线程“main”java.lang.NullPointerException中出现异常
位于java.lang.String.compareTo(String.java:1139)
在StudentRegistrar.idNumber(StudentRegistrar.java:152)
at Sort.main(Sort.java:21)

通过浏览您的代码,我猜您的数组大小可能超过了学生人数。如果是这种情况,您将尝试比较数组中的空插槽,这将导致空指针异常。要解决此问题,请将增量增加到nNumStudents,而不是数组的全长。

将出现此空指针,因为
String数组String[]iD=新字符串[150]的所有成员未初始化。例如,填充此iD数组的for循环在150之前未运行,或者其一个成员初始化为null so

首先打印并检查nNumStudents的值应该是150。然后确保分配给iD数组的每个值都是非空值,您可以通过修改代码来打印分配给它的所有值来实现这一点

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());
    }
for(int i=0;i

如果超过150,则您将获得一个
ArrayIndexoutofbound
exception not null指针

您获得的null指针是哪一行?与共享日志,并说明我在哪一行添加了错误消息以及提供错误的行…我不知道您在请求日志时请求的是什么??如果可以,请通知我。谢谢打印
j
的值。可能超过150。