私は長い間プロジェクトに取り組んできましたが、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)