简体   繁体   中英

Why is my program throwing by a null exception when reading this file input?

Writing a program to play the game Chutes and Ladders by inputting different ".txt" files and reading their data and outputting it to virtual game board. There is a lot more to the program, however, this part is hanging me up. Whenever the program tried to read the.txt file it throws an Exception and outputs the word "null".

   public static void main(String[] args) throws IOException {
      int[] boardGame = null;
      Scanner scnr = new Scanner(System.in);
      String fileName;
      String LogFile;
      
      try {
      System.out.println("Enter gameboard data input filename:");
      fileName = scnr.next(); 
     
      boardGame = createGameboard(fileName); //hangup appears to be here and throws Exception
      System.out.println();
      LogFile = writeLogFile(fileName, boardGame);
      if(LogFile == "null") {
      throw new Exception("Program will continue without a log file.");
      }
      else {
         System.out.println("Log file " + LogFile + " successfully created");
      }
      System.out.println();
      } catch (IOException excpt) {
         System.out.println(excpt.getMessage());
         System.out.println("Cannot play without gameboard, so program exiting");
      } catch (Exception excpt) {                 //which is caught here and displays the word null
         System.out.println(excpt.getMessage()); 
      }
      
   }
   
   public static int[] createGameboard(String nameFile) throws IOException {
      int[] gameBoard = null;
      File gameFile = new File(nameFile);
      Scanner inFS = new Scanner(gameFile);
      int boardSize;
      int numLadders = 0;
      int numChutes = 0;
      int index;
      int value;
      
      boardSize = inFS.nextInt();
      boardSize = boardSize;
      gameBoard = new int[boardSize];
      while(inFS.hasNextInt()) {
         index = inFS.nextInt();
         value = inFS.nextInt();
         gameBoard[index] = value;
         if (value > 0) {
            numLadders++;
         }
         else if(value < 0) {
            numChutes++;
         }
         else {
         }        
      }
      
      System.out.println("Gameboard setup with " + (boardSize - 1) + " squares");
      System.out.println("  " + numChutes + " squares have a chute");
      System.out.println("  " + numLadders + " squares have a ladder");
           
      return gameBoard;
      
   }
}
   

Sample ".txt" file being used: 31 (boardSize) 3 19 (square number: move up or down 'x' spaces) 5 3 11 15 20 9 17 -13 19 -12 21 -12 27 -26

I think the problem lies here

boardSize = inFS.nextInt();

Check if the inFS (File) has next input as int or not.

Correct it with

if(inFS.hasNextInt())
     boardSize = inFS.nextInt();

Sorry about the non-answer, but I am unable to reproduce your problem with the code you have provided.

I copied your code, added imports, a class definition, and a dummy writeLogFile method, then copied your sample text file and guessed at how you ran it. See the output below.

It would be really helpful if you did this experiment yourself before posting, because that way you can check that your question actually captures the problem, and has working steps to reproduce:

$ cat foo.txt
31
3 19
5 3
11 15
20 9
17 -13
19 -12
21 -12
27 -26

$ javac Foo.java && java Foo
Enter gameboard data input filename:
foo.txt
Gameboard setup with 30 squares
  4 squares have a chute
  4 squares have a ladder

Log file dummy.txt successfully created

$

Here's Foo.java :

import java.util.*;
import java.io.*;

class Foo {
   public static void main(String[] args) throws IOException {
      int[] boardGame = null;
      Scanner scnr = new Scanner(System.in);
      String fileName;
      String LogFile;
      
      try {
      System.out.println("Enter gameboard data input filename:");
      fileName = scnr.next(); 
     
      boardGame = createGameboard(fileName); //hangup appears to be here and throws Exception
      System.out.println();
      LogFile = writeLogFile(fileName, boardGame);
      if(LogFile == "null") {
      throw new Exception("Program will continue without a log file.");
      }
      else {
         System.out.println("Log file " + LogFile + " successfully created");
      }
      System.out.println();
      } catch (IOException excpt) {
         System.out.println(excpt.getMessage());
         System.out.println("Cannot play without gameboard, so program exiting");
      } catch (Exception excpt) {                 //which is caught here and displays the word null
         System.out.println(excpt.getMessage()); 
      }
      
   }

   static String writeLogFile(String x, int[] y) {
     return "dummy.txt";
   }
   
   public static int[] createGameboard(String nameFile) throws IOException {
      int[] gameBoard = null;
      File gameFile = new File(nameFile);
      Scanner inFS = new Scanner(gameFile);
      int boardSize;
      int numLadders = 0;
      int numChutes = 0;
      int index;
      int value;
      
      boardSize = inFS.nextInt();
      boardSize = boardSize;
      gameBoard = new int[boardSize];
      while(inFS.hasNextInt()) {
         index = inFS.nextInt();
         value = inFS.nextInt();
         gameBoard[index] = value;
         if (value > 0) {
            numLadders++;
         }
         else if(value < 0) {
            numChutes++;
         }
         else {
         }        
      }
      
      System.out.println("Gameboard setup with " + (boardSize - 1) + " squares");
      System.out.println("  " + numChutes + " squares have a chute");
      System.out.println("  " + numLadders + " squares have a ladder");
           
      return gameBoard;
      
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM