输入字符串的 NumberFormatException:“”

NumberFormatException for inputString: ""

我的 运行 我的 JavaFX 数独应用程序有问题。

我有一个方法 void updateBoard(),它使用 sudokuBoard 对象并将 textFields 中的数字分配给每个 sudokuBoard 字段。一个 textField 负责一个 sudokuBoard 字段。

public void updateBoard() {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if ((Integer) Integer.parseInt(textFields[i][j].getText()) != null) {
                    sudokuBoard.set(i, j, Integer.parseInt(textFields[i][j].getText()));
                } else {
                    sudokuBoard.set(i, j, null);
                }
                index++;
            }
        }
    } 

然后我还有一个方法 boolean checkBoard() 检查 sudokuBoard 是否被正确解决。

public boolean checkBoard() {
        updateBoard();
        int counter = 0;
        if (difficultyLevel.getLevel() == 0) {
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    if (counter % 5 == 0) {
                        if (sudokuBoard.getValue(i, j) == null) {
                            return false;
                        }
                        if (sudokuBoard.getValue(i, j) != difficultyLevel.getRejectedNumbers()[i][j]) {
                            return false;
                        }
                    }
                    counter++;
                }
            }
        }
        if (difficultyLevel.getLevel() == 1) {
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    if (counter % 3 == 0) {
                        if (sudokuBoard.getValue(i, j) == null) {
                            return false;
                        }
                        if (sudokuBoard.getValue(i, j) != difficultyLevel.getRejectedNumbers()[i][j]) {
                            return false;
                        }
                    }
                    counter++;
                }
            }
        }
        if (difficultyLevel.getLevel() == 2) {
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    if (counter % 2 == 0) {
                        if (sudokuBoard.getValue(i, j) == null) {
                            return false;
                        }
                        if (sudokuBoard.getValue(i, j) != difficultyLevel.getRejectedNumbers()[i][j]) {
                            return false;
                        }
                    }
                    counter++;
                }
            }
        }
        return true;
    }

当我从方法 checkBoard() 调用方法 updateBoard() 时,它可以正常工作。

当我调用此方法时(按下按钮后):

public void saveGameSudokuBoardButtonPushed() throws Exception {
    updateBoard();
    Stage stage = (Stage) saveGameSudokuBoardButton.getScene().getWindow();
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Zapisz plik");
    File file = fileChooser.showSaveDialog(stage);
    FileSudokuBoardDao fileSudokuBoardDao = null;
    try {
        fileSudokuBoardDao = (FileSudokuBoardDao) SudokuBoardDaoFactory.getFileDao(file.toPath().toString()); 
    } catch (NullPointerException exception) {
        exception.printStackTrace();
    }
    
    fileSudokuBoardDao.write(sudokuBoard);
}

我收到以下错误:

 Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.access00(Scene.java:3579)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent(GlassViewEventHandler.java:434)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
    ... 47 more
Caused by: java.lang.NumberFormatException: For input string: ""
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
    at java.base/java.lang.Integer.parseInt(Integer.java:662)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at gui.SudokuBoardController.updateBoard(SudokuBoardController.java:86)
    at gui.SudokuBoardController.saveGameSudokuBoardButtonPushed(SudokuBoardController.java:174)
    ... 58 more

Process finished with exit code 0

我不知道为什么在调用 saveGameSudokuBoardButtonPushed() 方法时会出现问题,而在调用 checkBoard() 时不会出现问题方法。

方法 saveGameSudokuBoardButtonPushed() 在内部没有调用方法 updateBoard() 时完美运行。

//编辑//

我应该补充一点,我的 sudokuBoard 已经实现了从 sudokuBoard 中删除某些字段的难度级别。那些删除的字段保存在那些难度级别 类 中,并且在它们的位置上 sudokuBoard 字段中有 null。然后将 sudokuBoard 字段分配给 GUI 上的 textFields。所以没关系,当我试图保存某些字段为空的板时。但是我想知道为什么这取决于我是否在 checkBoard() 方法和 saveGameSudokuBoardButtonPushed() 中使用 updateBoard() 方法,如果它们的实现相同

""无法解析成整数。因此,您需要跳过解析空白字符串。您可以按如下方式进行:

if (!textFields[i][j].getText().isBlank()) {
    sudokuBoard.set(i, j, Integer.parseInt(textFields[i][j].getText()));
}

我建议您将代码更改为如下内容:

String text=textFields[i][j].getText();
text=(text.trim().length()==0?"-1":text);
if ((Integer) Integer.parseInt(text) != -1) {
  sudokuBoard.set(i, j, text));
 } else {