Java, how can I avoid "may not have been initialized"

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?

+4
source share
5 answers
 ArrayList<Integer> gradeList = null; 

In your case, it is better to do something as follows:

 List<Integer> gradeList = examList.get(courseID); if(gradeList == null) { gradeList = new ArrayList<Integer>(); //... do something } else { //... do something else } 
+6
source

just initialize the List class when you create the variable.

 ArrayList<Integer> gradeList = new ArrayList<Integer>(); 

or set to null

 ArrayList<Integer> gradeList = null; 
+3
source

Assign null in your declaration:

 ArrayList<Integer> gradeList = null; 
+2
source

When you declare an ArrayList, just write gradeList = null . Or even better, to avoid NullPointerExceptions, initialize it in place, for example: ArrayList<Integer> gradeList = new ArrayList<Integer>();

+1
source

If the card contains a key, then the value for this key is your class. Just add this line under else if ( examList.containsKey(courseID)) { :

 gradeList = examList.get(courseID); 

Note that the designation of the examList map examList rather confusing. Why not examMap ? Also note that you can simply use else { instead of else if ( examList.containsKey(courseID)) { : either the map contains the course identifier or not; there is no other way. Finally, you will also have to fix the next line of code, because this is incorrect.

+1
source

Source: https://habr.com/ru/post/1379175/


All Articles