Why won't it compile? I am trying to sort a list of school courses by one int
property: courseLevel
, in ascending order.
I have a class named UCFCourse
with several objects courses[]
. I am assigning the property values to each object while incrementing x.Here is my code in my main:
courses[x] = new UCFCourse(courseCode, courseLevel, courseHours, replaceString, eitherCourse);
This is where I added my courses[]
.If I print out ListOne
I get a massive list containing all my courses.
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());
Comparator class:
import java.util.Comparator;
public class CourseComparator implements Comparator<UCFCourse> {
public int compare(UCFCourse Course1, UCFCourse Course2) {
return Course1.getCourseLevel() - Course2.getCourseLevel();
}
}
When I initially created my object it looked like this:
UCFCourse[] courses = new UCFCourse[75];
Not sure if this bit is relevant since I added all of them into an Array list already but I want to be thorough.
Errors:
Exception in thread "main" java.lang.NullPointerException