This discussion is archived
14 Replies Latest reply: Nov 12, 2010 11:22 AM by 796440 RSS

hashMap is returning Null Key

562805 Newbie
Currently Being Moderated
Hi,

this is my requirement

This program takes, as a single input, the name of a particular animal. The program should print out the animal’s name as well as a list of noises, or sounds, that this particular animal could make. Determine what, if anything should be modified in order to make this program work.

and here is the program
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class AnimalNoisesTest {
     
     public static Map<Animal, List<Noise>> animalNoises;
     
     public AnimalNoisesTest(){
          
          animalNoises = new HashMap<Animal, List<Noise>>();
          
          List noises = new ArrayList<Noise>();
          noises.add(new Noise("bark"));
          noises.add(new Noise("woof"));
                    
          animalNoises.put(new Animal("dog"), noises);
          
          noises.clear();
          noises.add(new Noise("meow"));
          noises.add(new Noise("hiss"));
          noises.add(new Noise("purr"));

          animalNoises.put(new Animal("cat"), noises);
          
          *System.out.println("key: " + animalNoises.get("cat"));*
          
          
          }

     /**
      * @param args
      * @return 
      */
     public static void main(String[] args) {
          String animal = args[0];
          
          AnimalNoisesTest animalNoisesTest = new AnimalNoisesTest();
          
          System.out.println("animalNoisesTest: " + animalNoisesTest.animalNoises.size());
          
          *if( animalNoisesTest.animalNoises.containsKey(animal)){*
               List<Noise> noises = animalNoisesTest.animalNoises.get(animal);
          
     
     for( Noise obj: noises ){
               
               System.out.println("A " + animal + "says " + ((Noise)obj).getNoise() + ".");
          }
          }
     }
     
     public class Animal{
          private String anAnimal;
          public Animal(String animalName){
               this.anAnimal = animalName;
          }
     }
     
     public class Noise{
          private String aNoise;
          
          public Noise(String noise){
               this.aNoise = noise;
          }
          
          public String getNoise(){
               return aNoise;
          }
          
          public void setNoise(String noise){
               this.aNoise = noise;
          }
     }
}
Edited by: qasitouch on Nov 12, 2010 9:34 AM

Edited by: qasitouch on Nov 12, 2010 9:36 AM

Edited by: qasitouch on Nov 12, 2010 9:44 AM
  • 1. Re: hashMap is returning Null Key
    796440 Guru
    Currently Being Moderated
    1. What specific problem are you having? Saying "hashMap is returning Null Key" means nothing.

    2. When posting code, surround it with [ code ] and [ /code ] (without the spaces inside the brackets) so it will be readable. Use the Preview tab to see how your post will look before submitting.
  • 2. Re: hashMap is returning Null Key
    562805 Newbie
    Currently Being Moderated
    THANKS FOR THE ADVICE BUT YOUR CODE COMMAND ALSO DON'T WORK,

    I JUST CHECKED THE FORMAT AND HERE IT IS
    USE THIS { code }

    THANk you,

    anyways still waiting for an answer
  • 3. Re: hashMap is returning Null Key
    DarrylBurke Guru Moderator
    Currently Being Moderated
    qasitouch wrote:
    THANKS FOR THE ADVICE BUT YOUR CODE COMMAND ALSO DON'T WORK,

    I JUST CHECKED THE FORMAT AND HERE IT IS
    USE THIS { code }
    Stop shouting and stop talking rubbish. [code]CODE[/code] will display as
    CODE
    anyways still waiting for an answer
    To what question?

    db
  • 4. Re: hashMap is returning Null Key
    796440 Guru
    Currently Being Moderated
    qasitouch wrote:
    THANKS FOR THE ADVICE BUT YOUR CODE COMMAND ALSO DON'T WORK,
    Yes it does. You just didn't follow my instructions correctly.

    Also, stop yelling. If someone was yelling at you, would you want to help him?
    I JUST CHECKED THE FORMAT AND HERE IT IS
    USE THIS { code }
    That also works.
    anyways still waiting for an answer
    Still waiting for a question.
  • 5. Re: hashMap is returning Null Key
    800268 Expert
    Currently Being Moderated
    The map doesn't return a null key. It returning a null value for the key "cat". Please show where you put a value into the map with that key.
  • 6. Re: hashMap is returning Null Key
    562805 Newbie
    Currently Being Moderated
    noises.add(new Noise("meow"));
              noises.add(new Noise("hiss"));
              noises.add(new Noise("purr"));
     
              animalNoises.put(new Animal("cat"), noises);
              
              *System.out.println("key: " + animalNoises.get("cat"));*
  • 7. Re: hashMap is returning Null Key
    796440 Guru
    Currently Being Moderated
    So, you think that
    new Animal("cat")
    and
    "cat"
    are equal?
  • 8. Re: hashMap is returning Null Key
    562805 Newbie
    Currently Being Moderated
    they are not, if i use this
    noises.add("bark");
    its ok for List noises

    but you see other code, i have to pass an onject of class animal, so if i don't i'll get errors
         animalNoises.put(new Animal("dog"), noises);
    can you please read the my 1st post and run this program for required output, you will have more clear idear.

    thanks in advance

    Edited by: qasitouch on Nov 12, 2010 10:23 AM
  • 9. Re: hashMap is returning Null Key
    796440 Guru
    Currently Being Moderated
    qasitouch wrote:
    they are not, if i use this
    Good. So if you do map.put(new Animal("cat", noises)), then you understand that map.get("cat") should return null, because you have not put a key of "cat".
    noises.add("bark");
    its ok for List noises
    No, it's not "ok". A List is not a Map. They are two completely different beasts, with different purposes, different behaviors, different methods.

    >
    but you see other code, i have to pass an onject of class animal, so if i don't i'll get errors
         animalNoises.put(new Animal("dog"), noises);
    Well, yeah, you have Map<Animal, List<Noise>>. That means that your keys must be Animals and your values must be Lists of Noise. Do you understand this?
    can you please read the my 1st post and run this program for required output, you will have more clear idear.
    I don't need to run it. I know exactly what the code will do.

    What I don't know is what real question you have.
  • 10. Re: hashMap is returning Null Key
    562805 Newbie
    Currently Being Moderated
    I want to put 2 key in hashmap

    one is "dog" and other one is "cat"

    animalNoises.put(new Animal("dog"), noises);
    how can i add these two keys in the hashmap so that when i run this code, i get th keys
    System.out.println("key: " + animalNoises.get("cat"));
  • 11. Re: hashMap is returning Null Key
    796440 Guru
    Currently Being Moderated
    qasitouch wrote:
    I want to put 2 key in hashmap

    one is "dog" and other one is "cat"

    animalNoises.put(new Animal("dog"), noises);
    how can i add these two keys in the hashmap so that when i run this code, i get th keys
    System.out.println("key: " + animalNoises.get("cat"));
    You can't.

    If you want get("cat") to return non-null, then you have to do put("cat", noises), not put(something which is not "cat" but which happens to have "cat" somewhere inside it, noises)

    The key that you use for put() has to be equal to the key that you use for get(). So if you're doing put(some_animal, noises), then when you call get(X), X.equals(some_animal) must be true.
  • 12. Re: hashMap is returning Null Key
    562805 Newbie
    Currently Being Moderated
    thnx half way done

    now i pass "cat" then i get noises related to cat, but when i pass "dog". i still get the cat noices

    can you please look into this code and tell me what wrong with it
                   List noises = new ArrayList<Noise>();
              noises.add(new Noise("bark"));
              noises.add(new Noise("woof"));
                        
              animalNoises.put("dog", noises);
    
              noises.clear();
              noises.add(new Noise("meow"));
              noises.add(new Noise("hiss"));
              noises.add(new Noise("purr"));
    
              animalNoises.put("cat", noises);
  • 13. Re: hashMap is returning Null Key
    DrClap Expert
    Currently Being Moderated
    You are using the same object for the cat and the dog.

    Apparently you have the fairly common mental model that adding an object to a map makes a copy of the object. That isn't the case. You're just adding a reference to the object, both times. Think of a "pointer" if "reference" is too abstract for you. So what happens is that at the end of that code is that the map contains two references to the "noises" object which at that point only contains the cat noises.

    So just use one object per animal. Don't try to reuse the object.
  • 14. Re: hashMap is returning Null Key
    796440 Guru
    Currently Being Moderated
    You have one List. When you call map.put(cat, noises), it does not copy that List. It's just a reference to that one List object. Then you're clearing that one List object and adding different Noises to it, but in your whole program, there's only one single List object.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points