So I'm trying to initialize a List of Maps, but every way I initialize it, it inits to null.
So I've tried googling and stack overflowing this, and there are some similar posts, but nothing explicitly like this. Just curious how I can init this so it isn't null, so I can add to it later in the code.
List<Map<String, Response>> listOfResponses = new ArrayList<Map<String, Response>>();
Then how I'm trying to add to list:
HashMap<String, Response> thisResponse = new HashMap<>();
int i = 1;
for (Question q : questions)
{
if( q == null)
{
return;
}
else {
q.DisplayQuestion();
mo.displayString("Please enter your response for the previous question: ");
Response r = Response.CreateResponse(mi.getNextString());
q.setResponse(r);
thisResponse.put(Integer.toString(i), r);
//q.setResponse(Response.CreateResponse(mi.getNextString()));
i++;
}
}
listOfResponses.add(thisResponse);
Save(this);
Error message is a nullpointer, because the list is initing to null, meaning it can't be added to.
Real Error message stack trace:
in thread "main" java.lang.NullPointerException
at com.company.Survey.TakeSurvey(Survey.java:304)
at com.company.Menu.SurveyMenu(Menu.java:129)
at com.company.Menu.SurveyMenu(Menu.java:78)
at com.company.Menu.StartMenu(Menu.java:24)
at com.company.Main.main(Main.java:21)