没有太多 Java 经验,但我目前正在编写一个非常基本的 JavaFX 应用程序,并尝试从 Excel 文件中读取信息。这一切都工作正常,但是,我遇到的问题是当尝试通过打开警报对话框而不是将它们打印到终端窗口来处理错误时。我目前已经得到了下面的代码,但无论出于何种原因,用户界面上没有任何反应,而是打印了下面的错误。任何帮助将不胜感激。#
protected void uploadNames() {
try {
FileChooser fileChooser = new FileChooser();
File selectedFile = fileChooser.showOpenDialog(stage);
FileInputStream file = new FileInputStream(selectedFile);
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet spreadsheet = workbook.getSheetAt(0);
for (int i = 0; i < spreadsheet.getLastRowNum(); i++) {
TextField textField = new TextField(spreadsheet.getRow(i).getCell(0).toString());
textField.setEditable(false);
namesList.getChildren().add(textField);
arrayNamesList.add(textField);
textField.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
namesList.getChildren().remove(textField);
arrayNamesList.remove(textField);
}
});
}
} catch (IOException e) {
Alert.AlertType alertType = Alert.AlertType.ERROR;
Alert alert = new Alert(alertType, "Error");
alert.getDialogPane().setContentText("Just checking this box open when error is found");
alert.showAndWait();
}
}
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
Exception in thread "JavaFX Application Thread" org.apache.poi.ooxml.POIXMLException: org.apache.xmlbeans.XmlException: Element presentation@http://schemas.openxmlformats.org/presentationml/2006/main is not a valid workbook@http://schemas.openxmlformats.org/spreadsheetml/2006/main document or a valid substitution.
at org.apache.poi.ooxml/org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:479)
at org.apache.poi.ooxml/org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:169)
at org.apache.poi.ooxml/org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:260)
at org.apache.poi.ooxml/org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:313)
at org.apache.poi.ooxml/org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:287)
at [email protected]/com.projects.firsttry.ApplicationController.uploadNames(ApplicationController.java:89)
at [email protected]/com.projects.firsttry.ApplicationController$3.handle(ApplicationController.java:142)
at [email protected]/com.projects.firsttry.ApplicationController$3.handle(ApplicationController.java:139)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189)
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.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:3984)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1890)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2708)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:937)
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:185)
at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: org.apache.xmlbeans.XmlException: Element presentation@http://schemas.openxmlformats.org/presentationml/2006/main is not a valid workbook@http://schemas.openxmlformats.org/spreadsheetml/2006/main document or a valid substitution.
at org.apache.xmlbeans/org.apache.xmlbeans.impl.store.Locale.autoTypeDocument(Locale.java:229)
at org.apache.xmlbeans/org.apache.xmlbeans.impl.store.Locale.lambda$parseToXmlObject$3(Locale.java:719)
at org.apache.xmlbeans/org.apache.xmlbeans.impl.store.Locale.syncWrap(Locale.java:491)
at org.apache.xmlbeans/org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:717)
at org.apache.xmlbeans/org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:234)
at org.apache.xmlbeans/org.apache.xmlbeans.impl.schema.AbstractDocumentFactory.parse(AbstractDocumentFactory.java:71)
at org.apache.poi.ooxml/org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:395)
... 38 more
我也尝试过使用抛出异常的方法,并尝试在调用该方法时执行 try 和 catch,但这产生了相同的结果。
POIXMLException
,但您正在捕获 IOException
。前者不是后者的子类型。而前者是未经检查的异常,这意味着编译器不会强迫你处理它。换句话说,您没有捕获 POIXMLException
,因此您的显示警报代码没有被执行。
如果您想处理
IOException
或 POIXMLException
那么您可以将后者添加到您的 catch
:
try {
...
} catch (IOException | POIXMLException e) {
...
}
请注意,这将阻止异常传播。由于您的
catch
块不会记录异常,因此控制台/日志中不会有堆栈跟踪。虽然警告用户出现问题是件好事,但我建议您仍然记录异常。这样您就可以看到哪里出了问题以及哪里。