爲什麼不編譯?我正在嘗試按照int
屬性:courseLevel
的升序對學校課程列表進行排序。修復我的排序錯誤
我有一個類名爲UCFCourse
與幾個對象courses[]
。我分配屬性值的每個對象而遞增x.Here是我在我的主要代碼:
courses[x] = new UCFCourse(courseCode, courseLevel, courseHours, replaceString, eitherCourse);
這是我加我courses[]
。如果我打印出來ListOne
我得到包含我的所有課程大規模列表。
List<UCFCourse> ListOne = new ArrayList<UCFCourse>();
for (int i = 0; i < courses.length; i++) {
ListOne.add(courses[i]);
}
//I added all my courses[] to a List
List<UCFCourse> ListOne = new ArrayList<UCFCourse>();
Collections.sort(ListOne, new CourseComparator());
比較類:
import java.util.Comparator;
public class CourseComparator implements Comparator<UCFCourse> {
public int compare(UCFCourse Course1, UCFCourse Course2) {
return Course1.getCourseLevel() - Course2.getCourseLevel();
}
}
當我最初創建我的對象時,它看起來像這樣:
UCFCourse[] courses = new UCFCourse[75];
不知道這一點是相關的,因爲我加入他們都成已經有數組列表,但我想徹底。
錯誤:
Exception in thread "main" java.lang.NullPointerException
你'null'-S在'ListOne'列表valiable。檢查你添加它們的位置。 –
您已發佈運行時錯誤,而不是編譯錯誤。 – shmosel
你是否使用'int'或'Integer'作爲'getCourseLevel()'的返回值? –