Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 显示FileChooser后按cancel会导致NullPointerException_Java_Nullpointerexception_Javafx 8_Filechooser - Fatal编程技术网

Java 显示FileChooser后按cancel会导致NullPointerException

Java 显示FileChooser后按cancel会导致NullPointerException,java,nullpointerexception,javafx-8,filechooser,Java,Nullpointerexception,Javafx 8,Filechooser,我有以下代码,当filechooser出现时,我可以按Choose选择哪个工作正常,导出工作正常,但当我按cancel时,它会导致NullPointerException: this.exportAnswersButton.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent actionEvent) { Fi

我有以下代码,当filechooser出现时,我可以按Choose选择哪个工作正常,导出工作正常,但当我按cancel时,它会导致NullPointerException:

this.exportAnswersButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("Select where to save the answers");
            fileChooser.setInitialFileName("" + selectedQuestionnaire.getTitle() + ".csv");
            String path = fileChooser.showSaveDialog(root.getScene().getWindow()).getPath();
            try
            {
                Exporter.exportQuestionnaireData(selectedQuestionnaire, path);
            }
            catch (NoQuestionnaireException e)
            {
                System.err.println("Tried to get a questionnaire that does not exist.");
            }
        }
    });
NullPointerException在
字符串路径=fileChooser.showSaveDialog(root.getScene().getWindow()).getPath()处引发正如堆栈跟踪告诉我的那样


是否有办法避免此异常?

showsavedilog
,返回:

null
如果未进行选择

因此,通过将
getFile()
链接到
showsavediao()
的末尾,当用户取消时,您将获得NPE。您需要执行以下操作:

File fileChoice = fileChooser.showSaveDialog(root.getScene().getWindow());
if (fileChoice == null) {
  // handle cancellation properly
}
else {
  String path = fileChoice.getPath();
  ...
}

它就发生在这里:exportQuestionnaireController.java:70。那节课,第70行。这意味着您在指针中调用的方法指向NULL,而不是实际对象。
File fileChoice = fileChooser.showSaveDialog(root.getScene().getWindow());
if (fileChoice == null) {
  // handle cancellation properly
}
else {
  String path = fileChoice.getPath();
  ...
}