Java - Adding Elements to Array of Hashmaps Produces Null Error -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i trying create 2d array of hashmaps. following code:
@suppresswarnings("unchecked") map<string, boolean>[][] arrayblock3 = (map <string, boolean>[][]) new map[6][6]; when try add elements array however, @ runtime there null pointer exception @ first line adding element array.
the code add element is:
arrayblock3[0][0].put("b", false); arrayblock3[0][1].put("g", false); arrayblock3[0][2].put("b", false); arrayblock3[0][3].put("b", false); arrayblock3[0][4].put("g", false); arrayblock3[0][5].put("b", false); error message
exception in thread "main" java.lang.nullpointerexception @ main.main(main.java:20) i appreciate help, thanks.
you need initialize each element in matrix of map (and need use class implements map, such hashmap):
for (int = 0; < arrayblock3.length; i++) { (int j = 0; j < arrayblock3[i].length; j++) { arrayblock3[i][j] = new hashmap<>(); } }
Comments
Post a Comment