I have a hashmap that contains multiple string arrays. I am trying to output each element in one of the arrays of the hashmap however I seem to always get
java.lang.NullPointerException
Here is my code,
import java.util.HashMap;
public class TestApp {
private static HashMap<String, String[]> subjects;
public TestApp() {
HashMap<String, String[]> subjects = new HashMap<String, String[]>();
subjects.put("calculus",new String[] {"math","logic"});
subjects.put("chemisty",new String[] {"ions","electrons"});
subjects.put("biology",new String[] {"life","bacteria"});
}
public static void main(String[] args){
for(String s:subjects.get("biology")){
System.out.println(s);
}
}
}
How can i stop this issue?
subjectsinside ofTestApp()which is unrelated to theprivate staticvariable.TestApp()? That code is not getting run in the first place.Either do all your code in
main(or related static functions), or do your code inTestApp()and just instantiate an instance inmain. For example: