0

I'm following this course on PluralSite and I am copying the code through and through however he is using netbeans and I'm using eclipse and I can't figure out what is happening. Is there a difference when using netbeans and ecplise to use an fxml file? I would appriciate if someone can help. If you have need any more info too help by all means ask. I'm new to javafx so be nice. The fxml file is in the same package as the others:

here is the exception i get, followed by the app code, and then the fxml code

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at application.SimpleUI.start(SimpleUI.java:18)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application application.SimpleUI





  //first is my app
    package application;

    import java.io.IOException;

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;

    public class SimpleUI extends Application {
        @Override
        public void start(Stage primaryStage) throws Exception {
            HBox box =        FXMLLoader.load(getClass().getResource("/hey/src/application/simpleui.fxml"));

    //      VBox vBox = new VBox(new Label("This goes down"), new Button("Down"), new Button("Further Down"));
    //
    //      box.getChildren().addAll(new Label("UserName"), new TextField(), new Button("Connect"), vBox);

            Scene scene = new Scene(box);
            primaryStage.setScene(scene);
            primaryStage.show();
        }

        public static void main(String[] args) {
            launch(args);
        }
    }



    //this is my fxml code
    import java.io.IOException;

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;

    public class SimpleUI extends Application {
        @Override
        public void start(Stage primaryStage) throws Exception {
            HBox box = FXMLLoader.load(getClass().getResource("/hey/src/application/simpleui.fxml"));

    //      VBox vBox = new VBox(new Label("This goes down"), new Button("Down"), new Button("Further Down"));
    //
    //      box.getChildren().addAll(new Label("UserName"), new TextField(), new Button("Connect"), vBox);

            Scene scene = new Scene(box);
            primaryStage.setScene(scene);
            primaryStage.show();
        }

        public static void main(String[] args) {
            launch(args);
        }
    }




<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import java.scene.*?>
<?import java.scene.control.*?>
<?import java.scene.layout.*?>

<?import javafx.scene.layout.*?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>

<HBox>
    <children>
        <Label text= "UserName" />
        <TextField />
        <Button text= "Connect" /> 
    </children>
</HBox>

Fixed: I fixed it thanks to the people who answered my question

  • What happens when you run your code? Does it give an error or something? – TM00 Feb 20 '17 at 07:30
  • yes I edited the question so you can see the exception, will looking this problem up I saw that maybe the fxml file was in the wrong directory, but it was not – Christian Moore Feb 20 '17 at 07:34
  • Show your project file structure. If `simpleui.fxml` is in the same package as `SimpleUI` class then `FXMLLoader.load(getClass().getResource("simpleui.fxml"));` will correctly load FXML file. – MBec Feb 20 '17 at 11:34
  • /hey/src/application/simpleui.fxml, /hey/src/application/SimpleUI.java, I copied the qualified name for both of these from eclipse it shows them in the same package – Christian Moore Feb 20 '17 at 16:01

1 Answers1

0

I'm not sure this will fix your problem but one difference I see between my typical way of loading fxml files and your posted code is the path in you loader namely:

        HBox box = FXMLLoader.load(getClass().getResource("/hey/src/application/simpleui.fxml"));

I normaly load fxml as such:

        AnchorPane page = (AnchorPane) FXMLLoader.load(PreloadController.class.getResource("PreloaderGUI.fxml"));

And ensure that my Controller and fxml file is in the same package. I assume the getResource method is the same between these examples, but the String path passed is different. Try and change it to simply the name of the fxml file and see if it works. i.e as follows:

    HBox box = FXMLLoader.load(getClass().getResource("simpleui.fxml"));
TM00
  • 1,330
  • 1
  • 14
  • 26
  • FXML and Controller doesn't have to be in the same package. Location of `Controller` is acquired from `fx:controller="path.to.Controller"` in FXML file. Loading FXML file is the same as loading any resource from class path. See: http://stackoverflow.com/a/2593175/6708868 – MBec Feb 20 '17 at 08:42