courtenayt
asked on
null pointer exception on Java HshMap after it is pulled from another HashMap
Hi,
I'm receiving a very bizarre null pointer error from a Java HashMap after I pull it out of another HashMap. I have a function (part of which is shown below). In it I have a while loop that loops through and puts Answer objects into a HashMap called am. After this happens, I've run a quick test to output the size of the am HashMap. This code works.
However, after the while loop I insert the am HashMap into another HashMap called fa. If, at this point, I get the HashMap back out of the fa HashMap and store it in am2, and try to get the size of am2, I get a java.lang.NullPointerExcep tion. I can't figure out why this is happening. I've also put in some test code to loop through the keys in the fa HashMap and print out the contents. It does this just fine and shows all of the data for everything that should be in am2.
If anyone has any ideas why it would stop working after I pull the am2 HashMap out of the fa HashMap, that would be great!
Thanks,
Courtenay
// Declare AnswerMap
HashMap am = new HashMap();
// Declare FormAnswers
HashMap fa = new HashMap();
java.util.Iterator i = questions.iterator();
while (i.hasNext()){
............
// Create an Answer object and insert the fid, ftid, qtid and
Answer a = new Answer(fid_string, ftid_string, qtid_string, adv);
// add the Answer object to the AnswerMap
// (declared before the while loop)
am.put(qtid_string, a);
// this output statement works fine
System.out.println("FormPr ocessingSe rvlet.java : storeSubmittedFormPageData (): am.size() = " + am.size());
// test to make sure the am has the a
Answer a2 = (Answer) am.get(qtid_string);
} //end while
// Add the AnswerMap to FormAnswers
// fa declared before while loop
fa.put(ftid_string, am);
// test loop through fa HashMap. This correctly shows all the data that should be available in am2
Set fa_keys = fa.keySet();
System.out.println("FormPr ocessingSe rvlet.java : storeSubmittedFormPageData (): fa_keys.size() = " + fa_keys.size());
java.util.Iterator i2 = fa_keys.iterator();
while (i2.hasNext()){
String key = (String) i2.next();
System.out.println("FormPr ocessingSe rvlet.java : storeSubmittedFormPageData (): fa_key/ftid = " + key);
System.out.println("FormPr ocessingSe rvlet.java : storeSubmittedFormPageData (): am = " + fa.get(key));
}
HashMap am2 = (HashMap) fa.get("ftid_string"); // this line has no errors
System.out.println("FormPr ocessingSe rvlet.java : storeSubmittedFormPageData (): am2.size() = " + am2.size()); // this line causes the exception.
I'm receiving a very bizarre null pointer error from a Java HashMap after I pull it out of another HashMap. I have a function (part of which is shown below). In it I have a while loop that loops through and puts Answer objects into a HashMap called am. After this happens, I've run a quick test to output the size of the am HashMap. This code works.
However, after the while loop I insert the am HashMap into another HashMap called fa. If, at this point, I get the HashMap back out of the fa HashMap and store it in am2, and try to get the size of am2, I get a java.lang.NullPointerExcep
If anyone has any ideas why it would stop working after I pull the am2 HashMap out of the fa HashMap, that would be great!
Thanks,
Courtenay
// Declare AnswerMap
HashMap am = new HashMap();
// Declare FormAnswers
HashMap fa = new HashMap();
java.util.Iterator i = questions.iterator();
while (i.hasNext()){
............
// Create an Answer object and insert the fid, ftid, qtid and
Answer a = new Answer(fid_string, ftid_string, qtid_string, adv);
// add the Answer object to the AnswerMap
// (declared before the while loop)
am.put(qtid_string, a);
// this output statement works fine
System.out.println("FormPr
// test to make sure the am has the a
Answer a2 = (Answer) am.get(qtid_string);
} //end while
// Add the AnswerMap to FormAnswers
// fa declared before while loop
fa.put(ftid_string, am);
// test loop through fa HashMap. This correctly shows all the data that should be available in am2
Set fa_keys = fa.keySet();
System.out.println("FormPr
java.util.Iterator i2 = fa_keys.iterator();
while (i2.hasNext()){
String key = (String) i2.next();
System.out.println("FormPr
System.out.println("FormPr
}
HashMap am2 = (HashMap) fa.get("ftid_string"); // this line has no errors
System.out.println("FormPr
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cheers,
Courtenay