簡體   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