2014-04-09 4 views
-2

저는 intro CS 클래스이며 게임을하는 프로그램을 작성하고 있습니다. 프레임 워크 클래스는 다운되었지만 실행하려고하면이 오류가 발생합니다. 게임 메서드에서 51 번 줄에 널 포인트가 있지만 null 포인트가 발생하는 곳을 찾을 수 없다고합니다. 다음은 네 개의 클래스에 대한 코드입니다.Cant find where [thread "main"의 예외 "java.lang.NullPointerException]이 적용됩니다.

public class Pog //THIS IS DONE 
{ 
public static void main (String [] Agrs) 
{ 
    PogPlayer human = new PogPlayer("Human"); 
    PogPlayer computer = new PogPlayer("Computer"); 

    PogGame game = new PogGame (human, computer); 
    game.play(); 

} // method main 

} // class Pog 

public class PogDice 
{ 
private int die1; // Stores the value (1-6) of die1. 
private int die2; // Stores the value (1-6) of die2. 

private Random rand; 

public PogDice() 
{ 
    rand = new Random(); 

} // default constructor 
public int roll() 
{ 
    int total = 0; 
    int turnTotal = 0; 
    String choice; 

    Scanner scan; 

    scan = new Scanner(System.in); 

    do { die1 = rand.nextInt(5)+1; 
     die2 = rand.nextInt(5)+1; 
     total = die1+die2; 
     System.out.println("Current score is: " + total + "\nRoll again?"); 
     choice = scan.nextLine(); 

    } while (choice == "yes" && hasDoubleOne() == false); 

    turnTotal = turnTotal + total; 
    return turnTotal; 

} // method rollDice 

public boolean hasDoubleOne() 
{ 
    boolean doubleOne = false; 
    if(die1 == 1 && die2 == 1) 
    doubleOne = true; 
    else 
    doubleOne = false; 
    return doubleOne; 

} // method hasDoubleOne 

public String toString() 
{ 
    return (die1 + ", " + die2); 

} // method toString 

} // class PogDice 

public class PogGame 
{ 
private PogPlayer human; 
private PogPlayer computer; 

/** 
* PogGame (constructor) 
* 
* @param PogPlayer (human) 
* @param PogPlayer (computer) 
*/ 
public PogGame (PogPlayer humanArg, PogPlayer computerArg) 
{ 
    PogPlayer human = humanArg; 
    PogPlayer comptuer = computerArg; 

} // method PogGame (constructor) 

public void play() 
{ 
    System.out.println("Hello, Welcome to Pog.\n\n This game takes the user "+ 
         "and puts them agaisnt the computer\n in a dice-based "+ 
         "game. Each player takes turns rolling two dice,\n "+ 
         "with their sum being the total points the "+ 
         "player gets.\n If you roll two 1s you lose all "+ 
         "points that turn.\n You have the option to turn "+ 
         "over the dice at any point\n during your turn to "+ 
         "keep all of your points.\n First one to 21 wins!.\n"); 
    human.getCurrent(); 




} // method play 

} // class pogGame 

public class PogPlayer 
{ 
private int current;  // Value of the current roll 
private int score;  // Player's point score. 

private PogDice dice; // Player's dice 
private String name;  // Player's name. 

public final int WINNER = 21; // number of points required to win 

public PogPlayer (String nameArg) 
{ 
    PogDice dice = new PogDice(); 
    score = 0; 

} // method PogPlayer (constructor) 

public int getCurrent() 
{ 
    int current; 
    current = dice.roll(); 
    return current; 
} // method getCurrent 

public PogDice getDice() 
{ 
    new PogDice(); 
    return new PogDice(); 

} // method getDice 

public int getScore() 
{ 
    score = current + score; 
    return score; 

} // method getScore 

public boolean hasWon() 
{ 
    boolean win; 
    if(score == WINNER) 
    { 
    win = true; 
    } 
    else 
    win = false; 
    return win; 
} // method hasWon 

public boolean rollDice() 
{ 
    return false; 

} // method rollDice 

} // class PogPlayer 

나는 human.getCurrent();에서 오류가 발생하고 있음을 확신합니다. 선.

+1

이 무슨 라인에서 NPE가 발생할 수 있습니다, PogPlayer 생성자에서

public PogGame (PogPlayer humanArg, PogPlayer computerArg) { human = humanArg; comptuer = computerArg; } 

같은 일이 dice와 함께 간다로 변경 :

그래서 같은에 코드를 변경? –

+0

51 번 라인이 무엇인지 알 수 있습니까? – djechlin

+0

스택 트레이스는 어디에 있습니까? – kolossus

답변

0
public static void main (String [] Agrs) 
{ 
    PogPlayer human = new PogPlayer("Human"); 
    PogPlayer computer = new PogPlayer("Computer"); 

    PogGame game = new PogGame (human, computer); 
    game.play(); 

} // method main 

"human"은 지역 변수입니다. playhuman.getCurrent()으로 전화하면 다른 사람입니다. 특히 아직 초기화되지 않은 null입니다.

0

humancomputer 변수를 실제로 PogGame 클래스에 올바르게 설정하지 않는 것은 생성자에서 새 변수를 만들고 있지만 인스턴스 변수가 null로 남아 있기 때문입니다.

private PogPlayer human; 
private PogPlayer computer; 

/** 
* PogGame (constructor)  
* 
* @param PogPlayer (human) 
* @param PogPlayer (computer) 
*/ 
public PogGame (PogPlayer humanArg, PogPlayer computerArg) 
{ 
    PogPlayer human = humanArg; // human is a new variable rather than the human instance variable 
    PogPlayer comptuer = computerArg; 

} 

그래서 당신은 인간이 초기화되지 않았습니다 년부터 NPE가 발생합니다 여기 human.getCurrent();를 호출 할 때.

public PogPlayer(String nameArg) { 
    dice = new PogDice();// instead of PogDice dice = new PogDice(); 
    score = 0; 

}