0

I am trying to write a simple Diet program using maps with this code. But I keep getting NPE which keeps interrupting the process at random.

What is wrong with my code? Why is that that I keep getting this error and how do I fix it?

class Diet {
  public static void main(String[] args) {

    Map < Integer, String > FandVMap = new HashMap < Integer, String > (15);

    FandVMap.put(1, "A bowl of Salad");

    /* .
     .
     .
    */

    FandVMap.put(12, "A Banana");

    //************************************************************

    Map < String, Integer > CaloryMap = new HashMap < String, Integer > (30);

    CaloryMap.put("An Orange", 30);
    .
    .
    .
    CaloryMap.put("A bowl of Salad", 30);

    Random randomGenerator = new Random();
    randomGenerator = new Random();
    int i = 0;
    //int rand;
    while (true) {
      Integer rand = 0;
      rand = randomGenerator.nextInt(12);

      String name = FandVMap.get(rand);
      System.out.println(name);
      Integer Calory = 0;
      Calory = CaloryMap.get(name); // This is where the problem occurs. <<========
      int Sum=0;
      Sum=Sum+Calory.intValue();
      System.out.println(Sum);

      if (Sum > 1000) {
        break;
      }
    }
  }
}

And here is the output I get:

A Peach
50
null
A bowl of Salad
30
A Nectarine
50
java.lang.NullPointerException
at Diet.main(gadas.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

The Calory Sum isn't working either.

3
  • Because your CaloryMap don't have value that you expect. Check that all names from FandVMap are presented in CaloryMap. Commented Jun 8, 2015 at 20:20
  • 1
    If you want to map a small ordinal integer value to a String, using HashMap is overkill. There's a structure for that, and it's called "array". Commented Jun 8, 2015 at 20:27
  • You will never leave the while loop because your sum will always be zeroed Commented Jun 8, 2015 at 20:35

3 Answers 3

6

The method randomGenerator.nextInt(int n) - returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive).

So I think at some point when randomGenerator.nextInt(int n) returns 0 then -

String name = FandVMap.get(rand)

returns null and hence -

Calory = CaloryMap.get(name); 

also evaluted to null

Moreover if you FandVMap map doesn't contains a key from range 1 to 12, then the error may occurred for more than one case. Suppouse your FandVMap map doesn't contain a key 4 then FandVMap.get(4) also returns null.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you.i fixed it. i cant believe Such a simple problem had confused me for several hours. and the sum problem was even worse!. tnx.
0

String name = FandVMap.get(rand);

This is very likely returning a null, which you don't check for. Also, your variable naming doesn't follow standard protocols, your variable instances should be camel cased starting with a lower case letter. For instance, CaloryMap should become caloryMap. It doesn't affect this directly but it's good to bring up.

Comments

0

Whith randomGenerator.nextInt(12) you get a random number from 0 (inclusive) to 12 (exlusive)

so if you get 0 FandVMap.get(rand) will be null because you don't have an entry whith 0 as key in the HashMap.

rand = randomGenerator.nextInt(12);

  String name = FandVMap.get(rand);
  System.out.println(name);
  Integer Calory = 0;
  Calory = CaloryMap.get(name); 

from JavaDoc: public int nextInt(int n) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.