繁体   English   中英

为什么我的程序在读取此文件输入时抛出 null 异常?

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

通过输入不同的“.txt”文件并读取其数据并将其输出到虚拟游戏板,编写一个程序来玩Chutes and Ladders游戏。 该程序还有很多内容,但是,这部分让我感到困惑。 每当程序试图读取 .txt 文件时,它都会抛出异常并输出单词“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;
      
   }
}
   

正在使用的示例“.txt”文件:31(boardSize)3 19(平方数:向上或向下移动'x'空格)5 3 11 15 20 9 17 -13 19 -12 21 -12 27 -26

我认为问题出在这里

boardSize = inFS.nextInt();

检查 inFS(文件)是否有下一个输入为 int。

纠正它

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

很抱歉没有回答,但我无法使用您提供的代码重现您的问题。

我复制了您的代码,添加了导入,一个 class 定义和一个虚拟writeLogFile方法,然后复制了您的示例文本文件并猜测您是如何运行它的。 请参阅下面的 output。

如果您在发布之前自己进行了此实验,那将非常有帮助,因为这样您可以检查您的问题是否真正解决了问题,并且有重现的工作步骤:

$ 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

$

这是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;
      
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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