为什么我的程序在读取此文件输入时抛出空异常?
Why is my program throwing by a null exception when reading this file input?
编写程序通过输入不同的“.txt”文件并读取它们的数据并将其输出到虚拟游戏板来玩滑梯和梯子游戏。该程序还有很多内容,但是,这部分让我很困惑。每当程序尝试读取 .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(棋盘尺寸)
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();
抱歉 non-answer,但我无法使用您提供的代码重现您的问题。
我复制了您的代码,添加了导入、class 定义和一个虚拟 writeLogFile
方法,然后复制了您的示例文本文件并猜测了您如何 运行 它。请参阅下面的输出。
如果您在发帖前自己做了这个实验,那将非常有帮助,因为这样您就可以检查您的问题是否确实抓住了问题,并且有重现的工作步骤:
$ 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;
}
}
编写程序通过输入不同的“.txt”文件并读取它们的数据并将其输出到虚拟游戏板来玩滑梯和梯子游戏。该程序还有很多内容,但是,这部分让我很困惑。每当程序尝试读取 .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(棋盘尺寸) 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();
抱歉 non-answer,但我无法使用您提供的代码重现您的问题。
我复制了您的代码,添加了导入、class 定义和一个虚拟 writeLogFile
方法,然后复制了您的示例文本文件并猜测了您如何 运行 它。请参阅下面的输出。
如果您在发帖前自己做了这个实验,那将非常有帮助,因为这样您就可以检查您的问题是否确实抓住了问题,并且有重现的工作步骤:
$ 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;
}
}