Help on Hashmaps

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marilyn Hart

    Help on Hashmaps

    Hi

    I hope there is someone out there that may be able to help me.

    I have a Hashmap called that contains Bank Account objects,
    it is made up as follows.

    Bank Acc Number - int
    Bank Acc Type - String
    Cust Number - int
    Cust Name -String
    Balance - double

    I am trying to create an Iterator that will search each object in turn and
    pass the Acc Type and Balance into a calculate Interest Method in another
    class which will return the interest due. Then output on a simple output
    dialog.

    This is the code I have but it keeps giving me a Null Pointer Exception
    error.

    Where am I going wrong?

    Iterator i = accList.keySet( ).iterator();
    while (i.hasNext())
    {
    TypesOfAccount tempAcc = ((TypesOfAccoun t)accList.get(i ));
    accType = tempAcc.getType (); //String
    balance = tempAcc.getBala nce(); //double
    interest = tempAcc.calcInt erest(accType, balance);
    total += interest; //double
    }
    JOptionPane.sho wMessageDialog( null,
    "Interest on all accounts is " + total,
    "Interest on All Accounts.",
    JOptionPane.OK_ OPTION);

    What are the differences between keySet, valueSet and entrySet?
    Which should I be using?

    TIA

    Paul


  • Ryan Stewart

    #2
    Re: Help on Hashmaps

    "Marilyn Hart" <marilyn-paul"NoSpam"@ha rt-giggins.freeser ve.co.uk> wrote in
    message news:cstmkq$7tb $1@newsg2.svr.p ol.co.uk...
    [...][color=blue]
    > This is the code I have but it keeps giving me a Null Pointer Exception
    > error.
    >
    > Where am I going wrong?
    >
    > Iterator i = accList.keySet( ).iterator();
    > while (i.hasNext())
    > {
    > TypesOfAccount tempAcc = ((TypesOfAccoun t)accList.get(i ));
    > accType = tempAcc.getType (); //String
    > balance = tempAcc.getBala nce(); //double
    > interest = tempAcc.calcInt erest(accType, balance);
    > total += interest; //double
    > }
    > JOptionPane.sho wMessageDialog( null,
    > "Interest on all accounts is " + total,
    > "Interest on All Accounts.",
    > JOptionPane.OK_ OPTION);
    >
    > What are the differences between keySet, valueSet and entrySet?
    > Which should I be using?
    >[/color]
    "accList.get(i) " will return the value in the HashMap to which the Iterator i is
    mapped. Unless you're doing something *very* unusual, I'd give you 100:1 odds
    that there is no such key in your Map. You want to replace that line (the first
    one in the loop) with "TypesOfAcc ount tempAcc = (TypesOfAccount ) i.next();". As
    a side note, you might want to rethink your naming. TypesOfAccount and tempAcc
    don't mean anything to me, at least.

    Read the HashMap docs for the specifics of the methods. Briefly, keySet returns
    a Set of all the keys in the map, entrySet returns a Set of all the entries
    (each entry is of type Map.Entry), and valueSet doesn't exist. The values
    method, however, returns a Collection of all the values.

    Finally, please do not post to comp.lang.java. That group is deprecated.
    Followups set to c.l.j.p.


    Comment

    Working...