我有一个ArrayLists值的HashMap,但是当我添加ArrayLists时HashMap保持为空,然后当我尝试获取ArrayList时引发NullPointerException。很困惑。ArrayList导致nullpointerexception的HashMap
Random rand = new Random();
HashMap<String,ArrayList<Integer>> hands = new HashMap<String,ArrayList<Integer>>();
HashMap<Integer, Boolean> deck = new HashMap<Integer, Boolean>();
for(int x=0;x<4;x++){
for(int y=0;y<4;y++){
hands.put(x+SUITS[x], new ArrayList<Integer>());
}
}
for(int x=0;x<4;x++){
for(int y=0;y<13;y++){
int randCard = rand.nextInt(52)+1;
if(!deck.containsKey(randCard)){
deck.put(randCard, true);
hands.get(x+cardSuit(randCard)).add(randCard);
}else y--;
}
}
如果不能看到_cardSuit()_方法,则很难排除故障,但显然没有值映射到您使用的密钥时。 – jahroy 2013-02-27 04:35:53
看着你的代码,没有理由期望你试图解引用的任何键都会出现。当_put_与您在_get_时使用的密钥完全不同时,您使用的是您使用的密钥。 – jahroy 2013-02-27 04:37:56