1

I am trying the following to update the HashMap with new value of student, please check where I'm missing something.

ArrayList<Student> tempStudentList = XMLParser.studentHashMap.get(currentSectionName);

if(studentList==null)
{

    Log.v(CURRENT_SCREEN,"created another student list for section name:"+currentSectionName);
    tempStudentList = new ArrayList<Student>();

} 
Log.v(CURRENT_SCREEN,"Added student to the list");

tempStudentList.add(currentStudent);

XMLParser.studentHashMap.put(currentSectionName, tempStudentList);
2
  • What is the problem you are seeing? Commented Jun 13, 2011 at 12:37
  • You haven't given us nearly enough context - we don't know what's going wrong, for example, or where student is coming from. Commented Jun 13, 2011 at 12:38

2 Answers 2

3

I think you wanted to write tempStudentList instead of studentList at the null check.

Sign up to request clarification or add additional context in comments.

Comments

1

You're checking the wrong variable for null. Also, Java collections are mutable, the array in studentHashMap is manipulated directly so you don't need to perform another put after every query on your map:

ArrayList<Student> tempStudentList = XMLParser.studentHashMap.get(currentSectionName);

if(tempStudentList == null)
{
    Log.v(CURRENT_SCREEN, "created another student list for section name:"+currentSectionName);
    tempStudentList = new ArrayList<Student>();
    XMLParser.studentHashMap.put(tempStudentList);
} 

tempStudentList.add(currentStudent);
Log.v(CURRENT_SCREEN,"Added student to the list");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.