2014-02-17 2 views
0
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)) 
    } 
} 
+0

전체 덱 클래스를 추가하겠습니다. – user2909717

+1

현재 코드에서 배열의 모든 요소는 'null'입니다 ... –

+0

덱 클래스의 83 행의 내용은 무엇입니까? – MondKin

답변

0

가장 좋은 방법은 아파치 코 몬즈 랭 인 StringUtils을 사용하는 것입니다.

이 경우 StringUtils.defaultString (deck [iCount])가 이동하는 것처럼 보입니다. null 인 경우 defaultString은 ""(빈 문자열)을 반환합니다. 당신이 인 StringUtils을 사용하지하기로 결정하는 경우

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#defaultString%28java.lang.String%29

이, 가장 좋은 방법은 항상로 .toString을하기 전에 널 (null)을 확인하는 것입니다().

1

기본 생성자에 배열에 개체가 추가되지 않았습니다. 다른 생성자에서와 같은 방식으로 배열의 모든 필드를 초기화해야합니다.

1

이 라인

for(iCount=0; iCount<52; iCount++) 
{ 
    Card c = new Card(iCount); 
} 

많은 영향을 미치지 않습니다. 새 Card 객체를 아무 곳이나 저장하지 않으므로 가비지 수집 프로세스에 의해 버려집니다. 그들은 아마도 뭔가 선을 말해야합니다.

for(iCount=0; iCount<52; iCount++) 
{ 
    deck[iCount] = new Card(iCount); 
} 
+0

완벽한! 고맙습니다!! – user2909717

0

올바르게 배열을 초기화하지만 생성자에에 새로운 Card의 할당되지 있어 :

당신은 각 카드의 인스턴스를
public Deck() 
{ 
    nextCard = 0; 
    deck = new Card[52]; 
    int iCount; 
    for(iCount=0; iCount<52; iCount++) 
    { 
     Card c = new Card(iCount); 
    } 
} 

... 다음 멀리 던져. 즉, deck 배열의 모든 요소는 기본값 인 null입니다.

관련 문제