0

I am trying to load an fxml file I built with scene builder but for some reason, I get a java.io.IOException error.

Here is the error I get:

/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home/bin/java --module-path /Users/anish/codingprojects/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=53528:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/anish/IdeaProjects/FBLA/out/production/FBLA:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx-swt.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.base.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.controls.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.fxml.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.graphics.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.media.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.swing.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.web.jar sample.Main
Exception in Application start method
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:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    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:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: javafx.fxml.LoadException: 
/Users/anish/IdeaProjects/FBLA/out/production/FBLA/sample/mainpage.fxml:35

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
    at sample.Main.start(Main.java:13)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Caused by: java.lang.IllegalArgumentException: Can not set javax.swing.text.html.ListView field sample.MainPageController.hoursLog to javafx.scene.control.ListView
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.base/java.lang.reflect.Field.set(Field.java:780)
    at javafx.fxml/javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1174)
    at javafx.fxml/javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:105)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:865)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:759)
    at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    ... 14 more
Exception running application sample.Main

Here is my main class code:

package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;

import java.io.IOException;

public class addHours {
    @FXML
    public void display()
    {
        try {
            Pane addHoursPane = FXMLLoader.load(getClass().getResource("addHours.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Here is my FXML file that I built using scene builder:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.TextFlow?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="518.0" prefWidth="704.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.MainPageController">
    <children>
        <MenuBar AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
            <menus>
                <Menu mnemonicParsing="false" text="File">
                    <items>
                        <MenuItem mnemonicParsing="false" text="Close" />
                    </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Edit">
                    <items>
                        <MenuItem mnemonicParsing="false" text="Delete" />
                        <MenuItem mnemonicParsing="false" text="Unspecified Action" />
                    </items>
                </Menu>
            </menus>
        </MenuBar>
        <SplitPane fx:id="splitPane" dividerPositions="0.23062015503875968" orientation="VERTICAL" prefHeight="518.0" prefWidth="704.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="155.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="28.0">
            <items>
                <AnchorPane maxHeight="300.0" minHeight="100.0" prefHeight="100.0" prefWidth="160.0" />
                <AnchorPane prefHeight="100.0" prefWidth="160.0">
                    <children>
                        <ListView fx:id="hoursLog" layoutX="136.0" layoutY="65.0" prefHeight="393.0" prefWidth="527.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                    </children>
                </AnchorPane>
            </items>
        </SplitPane>
        <Button fx:id="addHoursButton" layoutX="24.0" layoutY="329.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="115.0" text="Add Hours" AnchorPane.bottomAnchor="142.0" />
        <Button fx:id="settings" layoutX="24.0" layoutY="426.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="115.0" text="Settings" AnchorPane.bottomAnchor="82.0" />
        <ImageView fx:id="profilePicture" fitHeight="45.0" fitWidth="45.0" layoutX="1.0" layoutY="29.0" pickOnBounds="true" preserveRatio="true" AnchorPane.leftAnchor="2.0" AnchorPane.topAnchor="30.0" />
        <TextFlow fx:id="profileDetails" layoutX="2.0" layoutY="75.0" prefHeight="35.0" prefWidth="101.0" AnchorPane.leftAnchor="2.0" AnchorPane.topAnchor="77.0" />
    </children>
</AnchorPane>

And here is my project structure (Never made a resources tab because then I could not connect my controller with my fxml files) I'm just starting out with JavaFX so I am probably not seeing something common here but I have researched everywhere online and nothing seems to fix it. Btw I am using JavaFX 11.02, Java SDK 12 and IntelliJ IDE

  • You have defined a onActionEvent in the FXML and not declared it in the Controller OR that line of code in Main that points to the mainpage.fxml is wrong We have not used IntelliJ but the fxml file are not in the resource folder ? – Vector Nov 21 '19 at 01:43
  • 1
    @Grendel - I don't think that has anything to do with the question... – Zephyr Nov 21 '19 at 05:17

1 Answers1

2

You are importing the wrong ListView.

Look at the line with your actual Exception:

IllegalArgumentException: Can not set javax.swing.text.html.ListView field sample.MainPageController.hoursLog to javafx.scene.control.ListView

Usually this means your FXML is defining a JavaFX control, but your actual class is importing javax.swing.text.html.ListView instead of javafx.scene.control.ListView.

Your question does not include MainPageController.java, where this error is coming from, so I cannot answer definitively, but I'd look there.

Side Note: I do recommend learning the Java naming conventions and sticking to them in the future.

Zephyr
  • 9,885
  • 4
  • 28
  • 63