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?
Code :
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);
}
}
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:
Code :
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.
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.
Re: Problem w/ Hashmap. Null Pointer?
this is the line where I am getting the exception
Code :
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:
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.
Re: Problem w/ Hashmap. Null Pointer?
Quote:
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.
Re: Problem w/ Hashmap. Null Pointer?
Think I may have nailed it! in the following line:
Code :
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.
Re: Problem w/ Hashmap. Null Pointer?
Your posted code defined and filled one collection and returned a reference to another.