我洗牌的想法是生成一个随机数,然后在cardsToSelect这个List里面在该随机数的位置把元素取出来按顺序放进新建的List——washedList里面。
我想打印一下输出看看有没有成功,就报错Exception in thread "main" java.lang.NullPointerException
该怎么解决?
代码在下面
谢谢
cardsToSelect是什么?初始化了吗
public void washCard() { int size = cardsToSelect.size(); List<Card> washedList = new ArrayList<Card>(); for (int i = 0; i < size; i++) { Card temp = new Card(); do { temp = cardsToSelect.get(random.nextInt(size)); } while (washedList.contains(temp)); washedList.add(temp); } System.out.println("==========洗牌后=========="); for (int i = 0; i < size; i++) { System.out.print(washedList.get(i).type + washedList.get(i).num + ","); } }