Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Problem w/ Hashmap. Null Pointer?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem w/ Hashmap. Null Pointer?

    So basically, I have written this class to define a hashmap, with a method to access the values.
    This is pretty explanatory, it is used as an answer key for a larger "quiz" program.
    Whenever I try and run this, I get a "NullPointerException".
    The parameter I am giving is an int (that SHOULD correspond with the key in the hashmap no?)
    Any idea what is going wrong here?



    package jQuiz;
    import java.util.*;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;
     
    public class QuizAnswers {
     
    	//create our map
      private static final Map<Integer,Character> mp = DefineAnswers();
     
      private static Map<Integer, Character> DefineAnswers(){
    	   Map<Integer, Character> question = new HashMap<Integer, Character>();
    	   // Create our answer key 
    	   question.put(0,'B');
    	   question.put(1,'D');
    	   question.put(2,'C');
    	   question.put(3,'A');
    	   question.put(4,'A');
    	   question.put(5,'B');
    	   question.put(6,'D');
    	   question.put(7,'B');
    	   question.put(8,'C');
    	   question.put(9,'A');
    	   return Collections.unmodifiableMap(mp);
        }
     
       public static char getAnswer(int QuestionNum) {
    	   //return our result
    	   return mp.get(QuestionNum);
        }
     
    }


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem w/ Hashmap. Null Pointer?

    Also, I should probably state. I am aware that an Array would be a viable solution to my problem, but this is a school assignment and I want to push my studies a little.
    atm though, I am at a complete loss as to why the following has a null reference:

    QuizAnswers.getAnswer(1)
    QuizAnswers.getAnswer(2)
    QuizAnswers.getAnswer(3)

    should it not return the value that corresponds with the Integer key as defined in my map?
    note I have tried both "Integer" and "int" for the parameter of getAnswer, to no avail.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem w/ Hashmap. Null Pointer?

    You get a NPE when some variable you are using does not have a valid value. If you know which variable is null, then backtrack in the code to see why it does not have a valid value.

    If you do not know which variable is null, add a println just before the statement with the NPE and print the values of all the variables in the statement where the NPE occurs.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem w/ Hashmap. Null Pointer?

    this is the line where I am getting the exception
    System.out.println("Question1:" + QuizAnswers.getAnswer(0));
    it was a test line I added a while back to see if I could retrieve the values.

    So this is saying that the 0 is invalid? Could it be that I am somehow creating my hashmap wrong?

    One thought that just popped in to my head is that the:
    question.put(0,'B');
    is not even being added to the hashmap for some reason?

    Obviously I am very new to the implementation of map/hashmap so it is very possible I am doing it wrong, and just don't see where my mistake is.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem w/ Hashmap. Null Pointer?

    saying that the 0 is invalid?
    No. An int value passed to a method would not throw a NPE.
    The NPE would be because quizAnswers is null. To verify that print out the value of quizAnswers.

    What class is quizAnswers? What does the getAnswer() method?

    Post the full text of the error message. I think you are leaving off some info.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem w/ Hashmap. Null Pointer?

    Think I may have nailed it! in the following line:
    return Collections.unmodifiableMap(mp);
    Instead of using the var mp (the var assigned to MAP), I tried the variable "question" (the var assigned to the HASHMAP). I am now able to successfully acquire the values from my hashmap.

    I think I was just assuming that the mp var would somehow reference the hashmap.
    thx fr the help.


    note: the QuizAnswers is the class I posted in my OriginalPost.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem w/ Hashmap. Null Pointer?

    Your posted code defined and filled one collection and returned a reference to another.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Re: null pointer exception problem
    By tbarbone in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2011, 08:56 PM
  2. null pointer exception problem
    By vandrop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2011, 01:45 PM
  3. [SOLVED] Fixed Null Pointer Exception but still have another problem
    By javapenguin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 29th, 2010, 10:16 PM
  4. setText() NULL pointer excception problem
    By TempSpectre in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2010, 08:32 PM
  5. the infamous null pointer applet problem
    By wolfgar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 22nd, 2010, 08:09 PM