null pointer exception on Java HshMap after it is pulled from another HashMap

courtenayt
courtenayt used Ask the Experts™
on
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.NullPointerException.  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("FormProcessingServlet.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("FormProcessingServlet.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("FormProcessingServlet.java: storeSubmittedFormPageData(): fa_key/ftid = " + key);
                        System.out.println("FormProcessingServlet.java: storeSubmittedFormPageData(): am = " + fa.get(key));

            }


            HashMap am2 = (HashMap) fa.get("ftid_string");  // this line has no errors

       System.out.println("FormProcessingServlet.java: storeSubmittedFormPageData(): am2.size() = " + am2.size()); // this line causes the exception.

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Analyst Programmer
Commented:
I am guessing you do not need double quoting in the following line:
HashMap am2 = (HashMap) fa.get("ftid_string");

If fa does not contain a key called "ftid_string", then it will return null, so causing the nullpointerexception.

Please try if the following works for you:
HashMap am2 = (HashMap) fa.get(ftid_string);

Author

Commented:
Thanks so much!  Guess it just takes a 2nd set of eyes sometimes

Cheers,
Courtenay

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial