我的程序有问题。
package game;
import java.util.Random;
import javax.swing.ImageIcon;
public class deck {
private Card deckOfCards[];
private final int number_cards = 52;
private Random various;
private int currentCard;
private int actualCard;
public ImageIcon CardImage;
public deck() {
int value[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11 };
ImageIcon[] image = {
new ImageIcon("/root/Desktop/as.jpg"),
new ImageIcon("/root/Desktop/as.jpg"),
new ImageIcon("/root/Desktop/as.jpg"),
new ImageIcon("/root/Desktop/as.jpg"),
new ImageIcon("/root/Desktop/as.jpg"),
new ImageIcon("/root/Desktop/as.jpg"),
new ImageIcon("/root/Desktop/as.jpg"),
new ImageIcon("/root/Desktop/as.jpg"),
new ImageIcon("/root/Desktop/as.jpg"),
new ImageIcon("/root/Desktop/as.jpg"),
new ImageIcon("/root/Desktop/as.jpg"),
new ImageIcon("/root/Desktop/as.jpg"),
new ImageIcon("/root/Desktop/as.jpg"),
};
currentCard = 0;
deckOfCards = new Card[number_cards];
various = new Random();
for (int count = 0; count < deckOfCards.length; count++) {
deckOfCards[count] = new Card(Rank.values()[count % 13].toString(),Suit.values()[count / 13].toString(), value[count % 13], image[count % 13]);
}
}
public void shuffle() {
currentCard = 0;
for (int first = 0; first < deckOfCards.length; first++) {
int second = various.nextInt(number_cards);
Card temp = deckOfCards[first];
deckOfCards[first] = deckOfCards[second];
deckOfCards[second] = temp;
}
}
public Card giveCard() {
if (currentCard < deckOfCards.length)
return deckOfCards[currentCard++];
else
System.out.println("No more Cards.");
return null;
}
public int totalValue() {
int cardValue = (deckOfCards[actualCard].toInt());
return cardValue;
}
public ImageIcon getImage() {
return CardImage;
}
}
package game;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Card {
private final String faceCard;
private final String suit;
private final int number;
public ImageIcon image;
Card(String faceCard, String suit, int number_, ImageIcon image) {
this.faceCard = faceCard;
this.suit = suit;
this.number = number_;
this.image = image;
}
public String toString() {
return faceCard + " of " + suit + "\n" + "Value of card: " + number;
}
public int toInt() {
return number;
}
public void setImage(ImageIcon image) {
this.image = image;
}
public ImageIcon getImage() {
return image;
}
}
//swing part
ImageIcon pic = new ImageIcon(myDeckOfCards.getImage());
panel2.add(lab1);
panel2.add(new JLabel(pic));
panel2.add(card1);
panel2.add(pn1);
panel2.add(btn1);
答案 0 :(得分:0)
NullPointerException意味着某些内容未初始化。你指的是不存在的东西。检查堆栈跟踪。通常会有一个引发异常的行号。这可能会给你一些提示。
答案 1 :(得分:0)
看起来你正试图从myDeckOfCards获取图像,当它不存在时。您是否已声明并初始化myDeckOfCards实例?