我一直在努力工作,现在已经很久了,我正在运行一个nullpointerexception。我明白,当一个对象指向什么都不是。在Java中进行冒泡排序时出现这个错误。我无法弄清楚是什么导致这个异常,因此无法解决它。此代码的目的是按特定顺序对学生ID号数组进行排序,我已选择降序排列。用Java气泡排序的Nullpointerexception
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)
你得到空指针是什么行?共享日志,并且在特定的行上声明 – 2013-03-24 22:24:36
我在提供错误的行中添加了错误消息...我不知道当您询问日志时您要求什么?如果可以,请通知我。谢谢 – user2205621 2013-03-24 22:31:15
打印'j'的值。可能它超过了150。 – Maroun 2013-03-24 22:33:52