Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6747223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:24:59+00:00 2026-05-26T12:24:59+00:00

I have a HashMap called examList which stored exam grades of each course a

  • 0

I have a HashMap called examList which stored exam grades of each course a student took. The key of this hashmap is courseID, and the value is an array list gradeList which contains all grades a student got in a course. The problem is as follows:

// 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 gradeList, but I do not yet initialize it. Under IF I check if student has already done this exam before. If he hasn’t then new entry is created for hashmap, and gradeList is finally initialized. Under ELSE (where, it is considered that there is an element with gradeList already initialized) I just add new grade. However, that seems to be the problem. I can’t compile it because program presumes that gradeList is not yet initialized here.
So, how can I fix this? Or can I avoid it (since logically, gradeList will always be initialized under ELSE) through error handling, of which I know little about?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:25:00+00:00Added an answer on May 26, 2026 at 12:25 pm
    ArrayList<Integer> gradeList = null;
    

    In your case, it’s 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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a hashmap where every key has many values(stored in a arraylist). How
I have a convenience collection class in my Flex project called HashMap, which is
Hey i have an EmployeeStore which i have used a hashmap for this. The
I have HashMap 1, which contains 5 keys, all of which have Hashmaps as
I have a hashmap which I want to copy for other use. But whenever
I have a HashMap where the key is a word and the value is
I have a hashmap with some information(key and value) in a perl file. I
If I have a HashMap that looks like this: HashMap<String, MyObject> where the String
i have a parent class called Course, and two child class PostgradCourse and UndergradCourse.
I have an object Line which contains 2 objects of type Point called Point1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.