Using String Array in HashMap, Java

54,102

Solution 1

  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);
    }
}

Solution 2

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

Solution 3

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.

Share:
54,102
user1294188
Author by

user1294188

Updated on July 09, 2022

Comments

  • user1294188
    user1294188 almost 2 years

    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?