8

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?

4 Answers 4

17
  1. You've redefined a new local variable subjects inside of TestApp() which is unrelated to the private static variable.
  2. Where are you instantiating 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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

5

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

1 Comment

I didn't know that "static blocks" existed. That's really cool.
2

You declare subjects twice. One as a class member which is null and one as a local variable in your constructor. Your constructor shoild start with the following line to work with the class member:

subjects = new HashMap<String, String[]>();

And you need to create a new TestApp instance in main before your loop.

Comments

0

You have not created a TestApp object reference inside the main method.

public static void main(String[] args) {
    ta = new TestApp();
    for (String s : ta.subjects.get("biology")) {
        // do your thing
    }
}

Also, you have redeclared subjects inside the constructor. Use:

public TestApp() {
    subject = ... // not HashMap<String, String[]> subjects =
    // and so on as before
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.