2021年5月1日星期六

NullPointerException when reading a textfile in java

I am getting a null pointer exception when I invoke the function loadGame() and after a lot of debugging it is still not clear where the null value could be. I have a feeling that it may be due to the line Scanner scan = new Scanner(new FileInputStream(fileName)); in loadGame3() as that is the only reasonable line that could cause the whole thing to break but I don't know why scan would be null since the textfile does have values in it. So could you help me find what is causing the exception.

loadGame():

private boolean loadGame() {          Scanner kb = new Scanner(System.in);          Boolean validInput = false;          String fileName = " ";          while (!validInput) {              System.out.println("Enter a txt file ");              fileName = kb.next();              if (fileName.contains(".")) {                  if (fileName.substring(fileName.lastIndexOf(".") + 1).equals("txt")) {                      validInput = true;                  }              }          }          boolean validFile =controller.loadGame2(fileName);          if (validFile) {              populateLoadGame();          }          return validFile;      }  

loadGame2():

public boolean loadGame2(String fileName) {          try {              model.loadGame3(fileName);              return true;          } catch (FileNotFoundException ex) {              System.out.println("File Not Found");              return false;          }      }  

loadGame3():

public void loadGame3(String fileName) throws FileNotFoundException {          shipList.clear();          coordinatesesTried.clear();          Scanner scan = new Scanner(new FileInputStream(fileName));          while (scan.hasNext()) {              String name = scan.next();              if (!name.equals("Coordinates")) {                  shipList.add(importShips(scan));              }           }      }  

The route of the code is that loadGame() gets invoked which invokes loadGame2() and that in turn invokes loadGame3(). Also I believe that shipList and coordinatesTried eventhough they get cleared, I don't think that is causing the nullpointer exception since it is just a empty list that isn't directly affecting anything or involved in any sort of logic/dependence.

Stacktrace:

Exception in thread "main" java.lang.NullPointerException      at BattleShips.CLI.loadGame(CLI.java:128)      at BattleShips.CLI.newGameCLI(CLI.java:49)      at BattleShips.Main.main(Main.java:28)  

importShips(Scanner scan):

private Ship importShips(Scanner scan) {          List<Coordinates> position = new ArrayList<>();          int hitCounter = 0;          importCoordinates(position, scan);          for (int i = 0; i < position.size(); i++) {              if (position.get(i).isHit()) {                  hitCounter++;                  coordinatesesTried.add(position.get(i));              }          }          Ship ship = new Ship(position.size(), hitCounter, position, position.size() == hitCounter);          return ship;      }  

The importShips is what is called from loadGame3(), forgot to mention it previously.

https://stackoverflow.com/questions/67351825/nullpointerexception-when-reading-a-textfile-in-java May 02, 2021 at 07:48AM

没有评论:

发表评论