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?
Internally, the HashMap uses an Array, and it maps the labels to array indexes using a hash function.
String is as a key of the HashMap When you create a HashMap object and try to store a key-value pair in it, while storing, a hash code of the given key is calculated and its value is placed at the position represented by the resultant hash code of the key.
Arrays in Java use object identity to determine equality. If we create HashMap with byte array as the key, we'll be able to retrieve a value only using exactly the same array object.
We can also convert an array of String to a HashMap. Suppose we have a string array of the student name and an array of roll numbers, and we want to convert it to HashMap so the roll number becomes the key of the HashMap and the name becomes the value of the HashMap. Note: Both the arrays should be the same size.
subjects
inside of TestApp()
which is unrelated to the private static
variable.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 in TestApp()
and just instantiate an instance in main
. For example:
private static HashMap<String, String[]> subjects;
public TestApp() {
}
public static void main(String[] args){
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"});
for(String s:subjects.get("biology")){
System.out.println(s);
}
}
To set up the map to be available from a static method, you need to initialize it in a static block. Building it in a constructor won't prove anything, Java does not run that constructor before calling main
.
import java.util.HashMap;
public class TestApp {
private static HashMap<String, String[]> subjects;
static {
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);
}
}
}
Also as an aside since you seem to be a student, it's usually considered a good practice to program to interfaces when possible. i.e., we would prefer to declare private static Map<String, String[]> subjects;
over HashMap when there's no reason it needs to be a specific kind of Map
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With