Java数组,NullPointerException异常? [英] Java array, NullPointerException?
本文介绍了Java数组,NullPointerException异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我宣布两张牌:
卡卡1 =新卡('3',Card.Suit.clubs);
卡卡2 =新卡(T,Card.Suit.diamonds);
本作品:
手HAND1 =新的手();hand1.takeCard(卡1);
但为什么这个不行?这使我对下联一个NullPointerException异常:
手[] =手新手[2];手[0] .takeCard(卡2);
解决方案
您正在申报的2手的数组。这是刚刚建立的数组。然后,您需要实例化阵列内部的手的对象。
说
手[0] =新的手();
手[1] =新的手();
I declared two cards:
Card card1 = new Card('3', Card.Suit.clubs);
Card card2 = new Card('T', Card.Suit.diamonds);
This works:
Hand hand1 = new Hand();
hand1.takeCard(card1);
But why does this not work? It gives me a NullPointerException on second line:
Hand[] hand = new Hand[2];
hand[0].takeCard(card2);
解决方案
You are declaring an array of 2 hands. This is just setting up the array. You then need to instantiate the hand objects inside the array.
Say
hand[0] = new Hand();
hand[1] = new Hand();
这篇关于Java数组,NullPointerException异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文