Using String Array in HashMap, Java

I have a hashmap that contains multiple string arrays. I am trying to output each element to one of the hashmap arrays, but I always seem to 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 problem?

+3


source to share


4 answers


  • You have redefined a new local variable subjects

    internally TestApp()

    that is not associated with a variable private static

    .
  • Where are you instantiating TestApp()

    ? First of all, this code does not run.

Either do all your code in main

(or related static functions), or execute the code in TestApp()

and just instantiate 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);
    }
}

      

+11


source


To set up a map accessible from a static method, you need to initialize it in a static block. Plotting it in a constructor will not prove anything, Java does not run this 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, since you seem to be a student, it is generally considered good practice to program interfaces whenever possible. that is, we would prefer to declare private static Map<String, String[]> subjects;

by HashMap if there is no reason why it should be a specific kind of map

+4


source


You announce twice subjects

. One as a member of the class that is null and the other as a local variable in your constructor. Your shoild constructor starts with the following line to work with a class member:

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

      

And you need to create a new instance TestApp

in main

before your loop.

+2


source


You have not created an object reference TestApp

inside the method main

.

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

      

Also, you have overridden objects inside the constructor. Using:

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

      

0


source







All Articles