numberformatexception:“”

kognpnkq  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(371)

我在运行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.access$1200(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$2(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$3(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已经实现了diffuculty级别,从sudokuboard中删除了某些字段。这些删除的字段保存在那些难度级别的类中,在它们的位置上,sudokuboard字段中有空值。然后sudokuboard字段被分配给gui上的textfields。所以当我试图保存板时,有些字段是空的,这是可以的。但是,我想知道为什么这取决于我是否在checkboard()方法中使用updateboard()方法,以及savegamesudokuboardbuttonpushed()方法,如果它们的实现都相同的话

e4yzc0pl

e4yzc0pl1#

"" 无法解析为整数。因此,您需要跳过对空字符串的分析。您可以按以下步骤进行:

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

zy1mlcev2#

我建议你把代码改成这样:

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 {

相关问题