Don't have a lot of experience with Java but I'm currently coding a very basic JavaFX app and trying to read information from an excel file. This is all working fine, however, the issue I'm having is when trying to handle errors by having it open an Alert Dialog instead of printing them onto the terminal window. I've currently got the code below but for whatever reason, nothing happens on the UI and I get the errors below printed instead. Any help would be appreciated.#
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 com.projects.firsttry@1.0-SNAPSHOT/com.projects.firsttry.ApplicationController.uploadNames(ApplicationController.java:89)
at com.projects.firsttry@1.0-SNAPSHOT/com.projects.firsttry.ApplicationController$3.handle(ApplicationController.java:142)
at com.projects.firsttry@1.0-SNAPSHOT/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
I have also tried having the method with throws exception and tried doing try and catch when the method is called but this produced the same result.
The exception in the stack trace is a POIXMLException
, but you are catching IOException
. The former is not a subtype of the latter. And the former is an unchecked exception, which means the compiler does not force you to handle it. In other words, you are not catching the POIXMLException
, and so your show-alert code is not being executed.
If you want to handle either IOException
or POIXMLException
then you can add the latter to your catch
:
try {
...
} catch (IOException | POIXMLException e) {
...
}
Note this will stop the exception from propagating. Since your catch
block does not log the exception there will be no stack trace in the console/logs. While alerting the user that something went wrong is good, I recommend you still log the exception. That way you can see what went wrong and where.