Can't store Object in HashMap

2434 views java

I'm trying to create a SecureDataContainer with HashMap so defined: HashMap: ()>
Where KeyCouple is a class defined by me which contains the couple to access to the Vector associated to that couple. Now when i create a couple of HashMap in this method

private Map<KeyCouple,Vector<E>> DBUsers;

public void createUser(String Id, String passw) throws 
     UserAlreadyPresent {
     if(Id.isEmpty() || passw.isEmpty()) throw new IllegalArgumentException();
     if(existsUser(Id)) throw new UserAlreadyPresent(Id);

     KeyCouple u = new KeyCouple(Id, passw);
     DBUsers.put(u, new Vector<>());

Now, in main class, I Run the following code:

private static void testContainer(SecureDataContainer<String> container){
    try {
    } catch (UserAlreadyPresent e) {
            System.out.println("User already present");

To create the user "Name" with the Password "pwd". But When i put something in the Vector associated to the couple created using my "put" method:

public boolean put(String Owner, String passw, E data) throws NoUserException {
    if(Owner == null || passw == null || data == null) throw new NullPointerException();
    if(Owner.isEmpty() || passw.isEmpty()) throw new IllegalArgumentException();

    KeyCouple user = new KeyCouple(Owner,passw);
        Vector<E> aux = DBUsers.get(user);
        return aux.add(data);
        throw new NoUserException("No user");

In main class, I call the method:

    try {
        container.put("Name", "pwd", someData of type E);
    } catch (NoUserException e){
        abort("no user");

and it abort in every case, going even in the catch branch and printing "no user".

What does this means?

answered question

Try to override equals and hashcode for you KeyCouple class.

1 Answer


Your passwords are different. You created it with Pwd, and in your main class you use pwd

posted this

Have an answer?


Please login first before posting an answer.