Re: How to compute the number of occurance of each element in an
array list?
Michael Preminger wrote:
John wrote:
why do I have the following errors?
n in thread "main" java.lang.NullPointerException
at
multipleChoiceQuestion.setUserResponses(multipleChoiceQuestion.java:199)
at survey.takeSurveyOrQuiz(survey.java:120)
at surveyQuizBank.takeSurveyOrQuiz(surveyQuizBank.java:83)
at connection.finishQuestion(connection.java:262)
at terminal.processKeyboardInput(terminal.java:39)
at surveyQuizTester.main(surveyQuizTester.java:19)
I guess I didnot intialize the Map properly.
userResponses=new HashMap();
The initialization is O.K, so your problem probably lies somewhere else,
beyond the scope of this question.
....
if a key (that is automatically generated by the application) has not
been used yet, get() returns null, and you assign it with
put(Stringpattern, new Integer(1))
otherwise you increment the already existing value.
....
There is an obvious possibility in the last paragraph, failure to test
for, and handle, null result from get().
Patricia