0

I've been helped so often by StackOverFlow, but I haven't ever thought, I would write here myself, especially because I struggle with the English language I litte. But I'll try because I'm working myself through some video course on JavaFX, but I haven't been able to move on for at least two days, because I'm not able to solve this problem on my own. It's about building a connection between a Java(FX) main class, a controller class and an intermediate fxml file.

So, I tried to a establish a stage and a scene like this:

package abcxyz;

import java.net.URL;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class TestSBMain extends Application {
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        URL url = getClass().getResource("hallo.fxml");
        Parent root = FXMLLoader.load(url);
        Scene scene = new Scene (root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);}
}

As you see, I connected the start method with an fxml-file, which I built with the scene builder. Have a look, please:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" 
xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="abcxyz.TestController">
   <children>
      <Button fx:id="halloButton" layoutX="258.0" layoutY="187.0" mnemonicParsing="false" text="Hallo" />
   </children>
</AnchorPane>

This file, as you see, is linked to a controller class, which is supposed to control the established JavaFX-Button. Please be patient with me, when I show this one as well:

package abcxyz;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

public class TestController implements Initializable {
    @FXML
    private Button halloButton;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        halloButton.setDisable(true);
    }

}

But I receive this error message, I haven't been able to come over for two days:

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:564)
    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:564)
    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:832)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
    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 abcxyz.TestSBMain.start(TestSBMain.java:16)
    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:391)
    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)
Exception running application abcxyz.TestSBMain

My program is hierarchized like this:

enter image description here

Can you see, what the probem might be? I would be very thankful for a solution or some small advices! Thank you so much in advance! I'm sorry for the length of my (first) post! :-)

All the best, Christian

  • Follow the tutorial that is suitable for your needs. https://openjfx.io/openjfx-docs/ – SedJ601 Oct 19 '20 at 23:25
  • Thank you, @Sedrick. I've done that already, when I started with JavaFX. But I don't find there anything on how to use FXML. I learned that from that video tutorial - but it doesn't work unfortunately. – MagisterInformaticus Oct 19 '20 at 23:37
  • Download Netbeans [here](http://netbeans.apache.org/download/nb120/index.html). When you create a new project choose `FXML JavaFX Maven Archetype`. – SedJ601 Oct 20 '20 at 01:42

0 Answers0