My graded unit for college is to create a Crown & Anchor dice game in Java. Without having to go to much into the rules etc, here are some of the pointers that will be relevant..
- Players enter their name on startup, my program should check source folder for playerName.ser to load their game state.
- Players information on this file is their name, current stake and the bankers stakes (this information should be reloaded if the players saved file exists.
- If it does not exist, player is advised on this, and asked what stake they want to play with and a new Player object is created.
I'll post the code that I have for the restartGame() but I've getting errors that I have been struggling with a couple weeks now. Feel like I'm banging my head against a wall.
Any help is appreciated.
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);      
    }
After running the console application, I am asked the players name, but then the following errors occur..
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)
This line 96 code refers to the line of code in I have commented beside in the restartGame() code:
String input2 = input.next();
Pretty new to this guys bear with me...this is my variables declared at start of Game class..
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;
 
    