Error: Exception in thread "main" java.lang.NullPointerException
at Deck.toString(Deck.java:83)
at DeckDriver.main(DeckDriver.java:25)
나는이 오류 메시지가 나타나는 이유를 완전히 알 수 없습니다. d.toString
은 갑판 d를 설명하는 52 줄의 코드를 표시합니다. NPE에 방지하기 위해toString을 참조 할 때 NPE
데크 클래스
import java.util.Random;
public class Deck
{
private Card[] deck;
private int nextCard;
Face face;
Suit suit;
/**
* Default Constructor
*
* <hr>
* Date created: Feb 17, 2014
*
*
*/
public Deck()
{
nextCard = 0;
deck = new Card[52];
int iCount;
for(iCount=0; iCount<52; iCount++)
{
Card c = new Card(iCount);
}
}
/**
* Copy Constructor
*
* <hr>
* Date created: Feb 17, 2014
*
*
* @param existingDeck
*/
public Deck(Deck existingDeck)
{
int i;
for(i=0;i<52;i++)
{
this.deck[i] = existingDeck.deck[i];
}
}
/**
* toString
*
* <hr>
* Date created: Feb 17, 2014
*
* <hr>
* @return
* @see java.lang.Object#toString()
*/
public String toString()
{
int iCount = 0;
String description = "";
for(iCount=0; iCount<52;iCount++)
{
description += deck[iCount].toString();
}
return description;
}
/**
* Shuffles the deck
*
* <hr>
* Date created: Feb 17, 2014
*
* <hr>
*/
public void shuffle()
{
Random r = new Random();
nextCard = 0;
int i;
for(i=0;i<52;i++)
{
int x = r.nextInt(52);
Card c = new Card();
c=deck[x];
deck[x]=deck[i];
deck[i]=c;
}
}
/**
* Deals individual card.
*
* <hr>
* Date created: Feb 17, 2014
*
* <hr>
* @return
*/
public Card dealACard()
{
Card c;
c=deck[nextCard];
nextCard++;
return c;
}
public String dealAHand(int handSize)
{
int i;
String hand="";
for(i=0;i==handSize;i++)
{
hand+="" + dealACard();
}
return hand;
}
}
DeckDriver 클래스
public class DeckDriver
{
public static void main(String[]args)
{
Deck d = new Deck();
System.out.print(d.toString()); //(this is the DeckDriver.main(DeckDriver.java:25))
}
}
전체 덱 클래스를 추가하겠습니다. – user2909717
현재 코드에서 배열의 모든 요소는 'null'입니다 ... –
덱 클래스의 83 행의 내용은 무엇입니까? – MondKin