有人可以帮我查一下吗?
错误:
java.lang.NullPointerException
如何删除此错误?
我的主要班级:
public class deck {
public static void main(String[] args) {
Deck app = new Deck();
app.Deck();
}
}
我的甲板课程:
public class Deck {
public int TOTALCARDS;
Card[] d;
public int nH;
public void createDeck() {
String[] suitsArray = new String[4];
for(int i=0; i<numArray.length; i++) {
numArray[i] = i+1;
}
for (int i=0; i<13; i++) {
if (i!=11){
deck[i+25] = new Card(suitsArray[2], i+1);
}
else if(i > 10){
deck[i+25] = new Card(suitsArray[2], i+1);
}
}
for (int i=0; i<13; i++) {
deck[i+25].display();
}
}
这是卡类。
public class Card {
public String suit;
public int number;
public Card(String s, int n){
this.suit = s;
this.number = n;
}
public String words;
public String getTitle(){
String
if (number == 1){
words = " Ace";
}
else if (number ==3){
words = " Three";
}
else if (number ==4){
words = " Four";
}
else if (number ==5){
words = " Five";
}
else if (number ==6){
words = " Six";
}
else if (number ==7){
words = " Seven";
}
else if (number ==8){
words = " Eight";
}
else if (number ==9){
words = " Nine";
}
else if (number ==10){
words = " Ten";
}
else if (number ==11){
words = " Jack";
}
else if (number ==12){
words = " Queen";
}
else if (number ==13){
words = " King";
}
displayTitle = suit + words;
if (displayTitle.equalsIgnoreCase("Diamond Queen")){
displayTitle= "DIAMOND QUEEN";
}
return (displayTitle);
}
public void display(){
System.out.println("< " + get() + " >");
}
}
当我运行代码时,我得到了
Hearts Ace
Hearts Two
Hearts Three
Hearts Four
Hearts Five
Hearts Six
Hearts Seven
Hearts Eight
Hearts Nine
Hearts Ten
Hearts Jack
Exception in thread "main" java.lang.NullPointerException
at Deck.createDeck(Deck.java:57)
at Deck.<init>(Deck.java:9)
at deckMain.main(deckMain.java:5)
答案 0 :(得分:1)
编辑:请参阅下面的Kick Buttowski的评论,这至多是部分答案。
您正在致电Card.display()
for (int i=0; i<13; i++) {
deck[i+25].display();
}
反过来调用Card.getTitle()
public void display(){
System.out.println("< " + getTitle() + " >");
}
问题在于:
public String getTitle(){
String displayTitle = suit + words; // <-- Problem
if (number == 1){
words = " Ace";
}
// ...
在标记的行中,words
尚未设置,因此它是null
。访问其值将引发NullPointerException
。
正如Takendarkk指出的那样,通过在构造函数或声明中初始化代码,您可以通过对代码的最小更改来克服这一点:
public class Card {
// ...
String words = "";
或者,因为(据我们所知)words
仅在display()
中使用,请将其移至:
public void display() {
String words = ""; // Still need to be initialised
// ...
答案 1 :(得分:0)
编辑:你的卡片大小是51 ......它应该是52
if (i!=11){
deck[i+25] = new Card(suitsArray[2], i+1);
}
else if(i > 11){
deck[i+25] = new Card(suitsArray[2], i+1);
}
这并没有加起来,如果i = 11?你什么都不做
i!=11 = false and i > 11 == false;
这意味着else if(i> 11)永远不会执行。 你的代码也完全一样吗?
如果您尝试将所有52张卡片初始化,您可以执行以下操作:
for (int i=0;i<deck.size;i++) {
deck[i] = new Card(suitsArray[i/13], numArray[i % 13]);
编辑:经过对你正在做的事情的更多解释之后......
for (int i=0, j=0;j<deck.length;i++) {
if ((i/13) == 2 && (i%13) == 11)
continue;
deck[j++] = new Card( suitsArray[i/13], (i % 13));
}
卡也有(更清楚)
static String[] titles = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
和
public String getTitle() {
return suit + " " + titles[number];
}
答案 2 :(得分:0)
这是因为你在deck [11]中的值为null,因为你跳过了将一个Card实例放在该索引处。
修改:[should be deck[i + 25]]
,其中i = 11
;
关于指向变量字为空的答案作为NPE的原因。我怀疑这一点。我已经在我的机器上尝试了代码而且它没有给我NPE。 (另见Concatenating null strings in Java)