restartGame()方法返回错误

时间:2017-02-14 14:08:54

标签: java eclipse netbeans ide

我的大学评分单位是创建一个皇冠&在Java中锚定骰子游戏。无需深入了解规则等,这里有一些相关的指针..

  • 玩家在启动时输入他们的名字,我的程序应检查 playerName .ser的源文件夹以加载他们的游戏状态。
  • 此文件的玩家信息是他们的名字,当前赌注和银行家赌注(如果玩家保存文件存在,则应重新加载此信息。
  • 如果它不存在,建议玩家,并询问他们想要玩什么牌照,并创建一个新的玩家对象。

我发布了我对restartGame()的代码,但是我已经收到了几周以来一直在努力的错误。感觉就像是我把头撞在墙上。

感谢任何帮助。

public void restartGame() throws IOException
{
    //ask player for players name and save in playerName
    String playerName = ui.getName();

    System.out.println("Your name is : " + playerName); 

            File varTmpDir = new File("saves/" + playerName.toLowerCase() + ".ser");
            boolean exists = varTmpDir.exists();

            if(exists == true)
            {
               Player tempPlayer = null;

    try    
            {
                FileInputStream fileIn = new FileInputStream(playerName + ".ser");
                ObjectInputStream in = new ObjectInputStream(fileIn);
                tempPlayer = (Player) in.readObject();
                in.close();
                fileIn.close();
                String restartYN = "";
                    while(restartYN.equals("") || (restartYN.toUpperCase().charAt(0) != 'Y' && restartYN.toUpperCase().charAt(0) != 'N'))
            {
                System.out.println("Match found with £"+ tempPlayer.getStake() +" stake and £" + tempPlayer.getBanker() +" banker do you want to continue Y/N? ");
                restartYN = input.next();
            }
            if(restartYN.toUpperCase().charAt(0) == 'Y'){
                aPlayer = new Player(playerName,tempPlayer.getStake(),tempPlayer.getBanker());
            }   
             }catch(IOException i){
            System.out.println("Save corrupt starting new game. \n");
             }catch(ClassNotFoundException c) {
            System.out.println("Save corrupt starting new game. \n");
             }
            }

            int pStake = 0;
    do{
        System.out.print("No save loaded starting new game enter stake : £");
            String input2 = input.next();**//THIS IS THE ERROR LINE
        try {
            pStake = Integer.parseInt(input2);
            if(pStake <1){
                System.out.println("ERROR: must be a number above 0 with no decimal points less than 2147483648.");
            } 
        } catch (NumberFormatException ex){
            System.out.println("ERROR: must be a number above 0 with no decimal points less than 2147483648.");
        }
    }while(pStake<1);

    aPlayer = new Player(playerName,pStake,100);      
    }

运行控制台应用程序后,系统会询问玩家名称,但随后出现以下错误。

run: Please enter your name and press ENTER chris Your name is : chris Exception in thread "main" java.lang.NullPointerException at cadice.Game.restartGame(Game.java:96) at cadice.Game.play(Game.java:30) at cadice.CrownAndAnchor.main(CrownAndAnchor.java:14) No save loaded starting new game enter stake : £C:\Users\chris\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 7 seconds)

这一行96代码是指我在restartGame()代码旁边注释的代码行:

String input2 = input.next();

对于这些家伙来说很新鲜的熊我...这是我在游戏类开始时声明的变量..

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;

    public class Game 
    {
    private CADice[] dice;
    private Player aPlayer;
    private ArrayList<Bet> bets;
    public UserInterface ui = new UserInterface();
    public char option;
    public Scanner input;

1 个答案:

答案 0 :(得分:0)

你不是在任何地方实现输入。你需要像

这样的东西

input = new Scanner(System.in)