HashMap problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SagarDoke
    New Member
    • Feb 2008
    • 24

    HashMap problem

    Is it possible to put hashmap within a hashmap?

    My code is,

    while (rs.next())
    {
    i++;
    current_client_ id = rs.getInt(1);

    if(i==1)
    {
    clientIdMap.put (rs.getInt(1), callCenterIdMap .put(rs.getInt( 2), rs.getString(3) ));
    }
    else if(current_clie nt_id != previous_client _id)
    {
    callCenterIdMap = new HashMap<Integer , String>();
    clientIdMap.put (current_client _id, callCenterIdMap .put(rs.getInt( 2), rs.getString(3) ));
    }
    else if(current_clie nt_id == previous_client _id)
    {
    clientIdMap.put (current_client _id, callCenterIdMap .put(rs.getInt( 2), rs.getString(3) ));
    }
    previous_client _id = current_client_ id;
    }

    rs is a result set.

    clientIdMap and callCenterIdMap are the two hashmaps.
    but while getting the values it is throwing NullPointerExce ption.
    My code is right or any err is there. Please, help me. its showing key data of clientIdMap but showing "values" as null.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by SagarDoke
    Is it possible to put hashmap within a hashmap?
    Sure, why not? I didn't look at your code (it lacks the 'code' tags and is hardly
    readable). but the key as well as the value of a HashMap can be arbitrary
    objects and a Map is an arbitrary object ...

    kind regards,

    Jos

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Originally posted by SagarDoke
      Is it possible to put hashmap within a hashmap?
      [CODE=Java]clientIdMap.put (rs.getInt(1), callCenterIdMap .put(rs.getInt( 2), rs.getString(3) ));[/CODE]
      Do you realize the nesting here doesn't store callCenterIdMap as the value in clientIdMap, but rather rs.getString(3) as the value? It would help if you
      1. Used code tags
      2. posted a SSCCE: http://mindprod.com/jgloss/sscce.html
      3. described what you are trying to do, what your goal is.

      Comment

      Working...