I have a HashMap called examList that stores exams for every course a student takes. The key to this hash of the file is courseID , and the value is a list of gradeList arrays that contains all the classes that the student received in the course. The problem is this:
// Add a new course exam listing // where each course exam can be done a max 5 times protected void addExam(String courseID, int grade) { ArrayList<Integer> gradeList; // First check if course is in the list, if not add it if ( !examList.containsKey(courseID) ) { gradeList = new ArrayList<Integer>(); examList.put(courseID, gradeList); examList.get(gradeList.add(grade)); // If course is already on the list, check if max number of attempts has been reached, if not add new grade } else if ( examList.containsKey(courseID)) { if ( gradeList.size() <= 5 ) // THIS IS WHERE ERROR OCCURES examList.get(gradeList.add(grade)); // HERE ALSO else System.out.println("Maxim number of attempts has been reached."); } }
As you can see, I first define a classList, but I have not initialized it yet. In case I check if this exam has already passed. If he did not create a new entry for hashmap, and the List class is finally initialized. In ELSE (where, it is believed that there is an element with an already starting class), I just add a new class. However, this seems to be the problem. I cannot compile it because the program assumes that the List class has not yet been initialized here. So how can I fix this? Or can I avoid it (since logically, the List class will always be initialized in ELSE) using error handling, which I know little about?
source share