Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using String Array in HashMap, Java

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?

like image 350
user1294188 Avatar asked Mar 27 '12 22:03

user1294188


People also ask

Can array be used in HashMap?

Internally, the HashMap uses an Array, and it maps the labels to array indexes using a hash function.

Can we store string in HashMap?

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.

Can we use array as key in HashMap in Java?

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.

Can we convert string to HashMap in Java?

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.


2 Answers

  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);
    }
}
like image 100
mellamokb Avatar answered Sep 27 '22 16:09

mellamokb


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

like image 28
Affe Avatar answered Sep 27 '22 17:09

Affe