'JavaFX Controller not properly loading FXML file
I am programming a GUI for a ground base for piloting a drone using an FXML file created with Scene Builder. The Main file looks like this:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/ground/view/HMI.fxml"));
loader.setController(new RadarView());
Parent root = loader.load();
//Parent root = FXMLLoader.load(getClass().getResource("/ground/view/HMI.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("HMI");
primaryStage.setResizable(false);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Test adding lines
launch(args);
}
}
As I set the controller at the Main file, I do not set it on the FXML code. The FXML file is quite complex and extended, but its generic structure is an AnchorPane which is the parent of a HBox that subdivides the GUI in three columns:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.effect.ColorInput?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane id="HMIpane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1080.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1080.0" prefWidth="1920.0">
<children>
<VBox id="Left_Column" minHeight="1080.0" minWidth="500.0" style="-fx-background-color: GREY;">
<children>
<TitledPane id="autoPilotPanel" collapsible="false" minHeight="150.0" minWidth="500.0" text="Autopilot Modes">
<content>
<AnchorPane id="autoPilotPanelPane" minHeight="120.0" minWidth="496.0" style="-fx-background-color: GREY;">
<children>
<Button id="apSpeedBtn" fx:id="apSpeedBtn" disable="true" layoutX="14.0" layoutY="14.0" minHeight="40.0" minWidth="140.0" mnemonicParsing="false" style="-fx-background-color: GREY; -fx-border-color: BLACK;" text="SPEED" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0" />
<Button id="apAltBtn" fx:id="apAltBtn" disable="true" layoutX="178.0" layoutY="14.0" minHeight="40.0" minWidth="140.0" mnemonicParsing="false" style="-fx-background-color: GREY; -fx-border-color: BLACK;" text="ALTITUDE" AnchorPane.topAnchor="0.0" />
<Button id="apVSBtn" fx:id="apVSBtn" disable="true" layoutX="342.0" layoutY="14.0" minHeight="40.0" minWidth="140.0" mnemonicParsing="false" style="-fx-background-color: GREY; -fx-border-color: BLACK;" text="VERTICAL SPEED" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
<Button id="apDisengageBtn" fx:id="apDisengageBtn" layoutX="276.0" layoutY="61.0" minHeight="40.0" minWidth="140.0" mnemonicParsing="false" style="-fx-background-color: #6e0000; -fx-border-color: BLACK;" text="DISENGAGE" textFill="RED" AnchorPane.bottomAnchor="0.0" />
<Button id="apEngageBtn" fx:id="apEngageBtn" layoutX="80.0" layoutY="72.0" minHeight="40.0" minWidth="140.0" mnemonicParsing="false" style="-fx-background-color: #0a3c00; -fx-border-color: BLACK;" text="ENGAGE" textFill="#2fff00" />
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane id="DLCommandLinePanel" collapsible="false" minHeight="565.0" minWidth="500.0" style="-fx-background-color: BLACK;" text="Data Link Command Line">
<content>
<AnchorPane id="DLCommandLinePanelPane" minHeight="532.0" minWidth="519.0" style="-fx-background-color: BLACK;">
<children>
<TextArea id="DataLinkCommandLog" fx:id="DataLinkCommandLog" editable="false" layoutX="18.6" layoutY="19.6" minHeight="400.0" minWidth="480.0" style="-fx-border-color: GREY;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<effect>
<ColorInput />
</effect>
</TextArea>
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane id="PFDPanel" collapsible="false" minHeight="365.0" minWidth="500.0" text="Primary Flight Data">
<content>
<AnchorPane id="PFDPanelPane" minHeight="125.0" minWidth="500.0" style="-fx-background-color: GREY;">
<children>
<GridPane id="PFDGrid" layoutX="64.0" layoutY="38.0" minHeight="175.0" minWidth="260.0" style="-fx-background-color: GREY;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label id="IASlabel" alignment="CENTER" contentDisplay="CENTER" prefHeight="79.0" prefWidth="120.0" style="-fx-border-color: WHITE;" text="IAS" textAlignment="CENTER" textFill="WHITE">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="altitudeLabel" alignment="CENTER" contentDisplay="CENTER" prefHeight="79.0" prefWidth="120.0" style="-fx-border-color: WHITE;" text="ALTITUDE" textAlignment="CENTER" textFill="WHITE" GridPane.rowIndex="1">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="VSlabel" alignment="CENTER" contentDisplay="CENTER" prefHeight="79.0" prefWidth="120.0" style="-fx-border-color: WHITE;" text="VS" textAlignment="CENTER" textFill="WHITE" GridPane.rowIndex="2">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="headingLabel" alignment="CENTER" contentDisplay="CENTER" prefHeight="79.0" prefWidth="120.0" style="-fx-border-color: WHITE;" text="HEADING" textAlignment="CENTER" textFill="WHITE" GridPane.rowIndex="3">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="AoAlabel" alignment="CENTER" contentDisplay="CENTER" prefHeight="79.0" prefWidth="120.0" style="-fx-border-color: WHITE;" text="AoA" textAlignment="CENTER" textFill="WHITE" GridPane.rowIndex="4">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="QNHlabel" alignment="CENTER" contentDisplay="CENTER" prefHeight="79.0" prefWidth="120.0" style="-fx-border-color: WHITE;" text="QNH" textAlignment="CENTER" textFill="WHITE" GridPane.rowIndex="5">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="IASvalue" alignment="CENTER" contentDisplay="CENTER" prefHeight="79.0" prefWidth="120.0" style="-fx-border-color: WHITE;" textAlignment="CENTER" textFill="WHITE" GridPane.columnIndex="1">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="altitudeValue" alignment="CENTER" contentDisplay="CENTER" prefHeight="79.0" prefWidth="120.0" style="-fx-border-color: WHITE;" textAlignment="CENTER" textFill="WHITE" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="VSvalue" alignment="CENTER" contentDisplay="CENTER" prefHeight="79.0" prefWidth="120.0" style="-fx-border-color: WHITE;" textAlignment="CENTER" textFill="WHITE" GridPane.columnIndex="1" GridPane.rowIndex="2">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="headingValue" alignment="CENTER" contentDisplay="CENTER" prefHeight="79.0" prefWidth="120.0" style="-fx-border-color: WHITE;" textAlignment="CENTER" textFill="WHITE" GridPane.columnIndex="1" GridPane.rowIndex="3">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="AoAvalue" alignment="CENTER" contentDisplay="CENTER" prefHeight="79.0" prefWidth="120.0" style="-fx-border-color: WHITE;" textAlignment="CENTER" textFill="WHITE" GridPane.columnIndex="1" GridPane.rowIndex="4">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="QNHvalue" alignment="CENTER" contentDisplay="CENTER" prefHeight="79.0" prefWidth="120.0" style="-fx-border-color: WHITE;" textAlignment="CENTER" textFill="WHITE" GridPane.columnIndex="1" GridPane.rowIndex="5">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="IAStarget" alignment="CENTER" contentDisplay="CENTER" prefHeight="76.0" prefWidth="134.0" style="-fx-background-color: GREY; -fx-border-color: WHITE;" textAlignment="CENTER" textFill="#ea00ff" GridPane.columnIndex="2">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="altitudeTarget" alignment="CENTER" contentDisplay="CENTER" prefHeight="76.0" prefWidth="134.0" style="-fx-background-color: GREY; -fx-border-color: WHITE;" textAlignment="CENTER" textFill="#ea00ff" GridPane.columnIndex="2" GridPane.rowIndex="1">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="VStarget" alignment="CENTER" contentDisplay="CENTER" prefHeight="76.0" prefWidth="134.0" style="-fx-background-color: GREY; -fx-border-color: WHITE;" textAlignment="CENTER" textFill="#ea00ff" GridPane.columnIndex="2" GridPane.rowIndex="2">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="headingTarget" alignment="CENTER" contentDisplay="CENTER" prefHeight="76.0" prefWidth="134.0" style="-fx-background-color: GREY; -fx-border-color: WHITE;" textAlignment="CENTER" textFill="#ea00ff" GridPane.columnIndex="2" GridPane.rowIndex="3">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="bankAngleLabel" alignment="CENTER" contentDisplay="CENTER" prefHeight="79.0" prefWidth="120.0" style="-fx-border-color: WHITE;" text="BANK ANGLE" textAlignment="CENTER" textFill="WHITE" GridPane.columnIndex="2" GridPane.rowIndex="4">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="QFElabel" alignment="CENTER" contentDisplay="CENTER" prefHeight="79.0" prefWidth="120.0" style="-fx-border-color: WHITE;" text="QFE" textAlignment="CENTER" textFill="WHITE" GridPane.columnIndex="2" GridPane.rowIndex="5">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="bankAngleValue" alignment="CENTER" contentDisplay="CENTER" prefHeight="79.0" prefWidth="120.0" style="-fx-border-color: WHITE;" textAlignment="CENTER" textFill="WHITE" GridPane.columnIndex="3" GridPane.rowIndex="4">
<font>
<Font size="15.0" />
</font>
</Label>
<Label id="QFEvalue" alignment="CENTER" contentDisplay="CENTER" prefHeight="79.0" prefWidth="120.0" style="-fx-border-color: WHITE;" textAlignment="CENTER" textFill="WHITE" GridPane.columnIndex="3" GridPane.rowIndex="5">
<font>
<Font size="15.0" />
</font>
</Label>
</children>
</GridPane>
</children>
</AnchorPane>
</content>
<VBox.margin>
<Insets />
</VBox.margin>
</TitledPane>
</children>
</VBox>
<VBox id="Center_Column" minHeight="1080.0" minWidth="920.0" style="-fx-background-color: BLACK;">
<children>
<TitledPane id="HNAVPanel" collapsible="false" minHeight="1040.0" minWidth="920.0" style="-fx-background-color: BLACK;" text="HNAV Panel">
<content>
<AnchorPane id="HNAVPanelPane" fx:id="HNAVPanelPane" minWidth="920.0" style="-fx-background-color: BLACK;" />
</content></TitledPane>
<ToolBar id="toolBar" minHeight="40.0" minWidth="920.0" style="-fx-background-color: BLACK;">
<items>
<Button id="checkListBtn" fx:id="a6" minHeight="26.0" minWidth="223.0" mnemonicParsing="false" style="-fx-border-color: #434343; -fx-background-color: BLACK;" text="CheckList" textFill="WHITE" />
<Button id="flightPlanBtn" fx:id="a7" minHeight="26.0" minWidth="223.0" mnemonicParsing="false" style="-fx-border-color: #434343; -fx-background-color: BLACK;" text="Flight Plan" textFill="WHITE" />
<Button fx:id="a8" minHeight="26.0" minWidth="223.0" mnemonicParsing="false" style="-fx-border-color: #434343; -fx-background-color: BLACK;" styleClass="dataLinkBtn" text="Data Link Status" textFill="WHITE" />
<Button id="weatherBtn" minHeight="26.0" minWidth="223.0" mnemonicParsing="false" style="-fx-border-color: #434343; -fx-background-color: BLACK;" text="Weather" textFill="WHITE" />
</items>
</ToolBar>
</children>
</VBox>
<VBox id="Right_Column" minHeight="1080.0" minWidth="500.0" style="-fx-background-color: GREY;">
<children>
<TitledPane id="VNAVPanel" collapsible="false" minHeight="500.0" minWidth="500.0" text="VNAV Panel">
<content>
<AnchorPane id="VNAVPanelPane" minHeight="180.0" minWidth="200.0" style="-fx-background-color: BLACK;" />
</content>
</TitledPane>
<TitledPane id="alertPanel" collapsible="false" minHeight="240.0" minWidth="500.0" text="Alert Panel">
<content>
<AnchorPane id="alertPanelPane" minHeight="209.0" minWidth="498.0" style="-fx-background-color: GREY; -fx-border-color: YELLOW;" />
</content>
</TitledPane>
<TitledPane id="actionPanel" collapsible="false" minHeight="340.0" minWidth="500.0" prefHeight="340.0" prefWidth="500.0" text="Action Panel">
<content>
<AnchorPane id="actionPanelPane" minHeight="310.0" minWidth="500.0" style="-fx-background-color: GREY;">
<children>
<Button id="directToBtn" fx:id="a11" layoutX="14.0" layoutY="14.0" minHeight="40.0" minWidth="150.0" mnemonicParsing="false" style="-fx-background-color: GREY; -fx-border-color: BLACK;" text="DIRECT TO" AnchorPane.leftAnchor="0.0" />
<Button id="headToBtn" fx:id="a12" layoutX="14.0" layoutY="66.0" minHeight="40.0" minWidth="150.0" mnemonicParsing="false" style="-fx-background-color: GREY; -fx-border-color: BLACK;" text="HEAD TO" AnchorPane.leftAnchor="0.0" />
<Button id="climbToBtn" fx:id="a13" layoutX="14.0" layoutY="118.0" minHeight="40.0" minWidth="150.0" mnemonicParsing="false" style="-fx-background-color: GREY; -fx-border-color: BLACK;" text="CLIMB TO" AnchorPane.leftAnchor="0.0" />
<Button id="descentToBtn" fx:id="a14" layoutX="14.0" layoutY="171.0" minHeight="40.0" minWidth="150.0" mnemonicParsing="false" style="-fx-background-color: GREY; -fx-border-color: BLACK;" text="DESCENT TO" AnchorPane.leftAnchor="0.0" />
<Button id="speedAtBtn" fx:id="a15" layoutX="14.0" layoutY="223.0" minHeight="40.0" minWidth="150.0" mnemonicParsing="false" style="-fx-background-color: GREY; -fx-border-color: BLACK;" text="SPEED AT" AnchorPane.leftAnchor="0.0" />
<ChoiceBox id="directToChoice" disable="true" layoutX="174.0" layoutY="14.0" minHeight="40.0" minWidth="315.0" style="-fx-background-color: GREY; -fx-border-color: BLACK;" AnchorPane.rightAnchor="0.0" />
<ChoiceBox id="headToChoice" disable="true" layoutX="174.0" layoutY="66.0" minHeight="40.0" minWidth="315.0" style="-fx-background-color: GREY; -fx-border-color: BLACK;" AnchorPane.rightAnchor="0.0" />
<ChoiceBox id="climbToChoice" disable="true" layoutX="174.0" layoutY="118.0" minHeight="40.0" minWidth="315.0" style="-fx-background-color: GREY; -fx-border-color: BLACK;" AnchorPane.rightAnchor="0.0" />
<ChoiceBox id="descentToChoice" disable="true" layoutX="174.0" layoutY="171.0" minHeight="40.0" minWidth="315.0" style="-fx-background-color: GREY; -fx-border-color: BLACK;" AnchorPane.rightAnchor="0.0" />
<ChoiceBox id="speedAtChoice" disable="true" layoutX="174.0" layoutY="223.0" minHeight="40.0" minWidth="315.0" style="-fx-background-color: GREY; -fx-border-color: BLACK;" AnchorPane.rightAnchor="0.0" />
<Button id="submitBtn" fx:id="a10" disable="true" layoutX="339.0" layoutY="271.0" minHeight="35.0" minWidth="150.0" mnemonicParsing="false" style="-fx-background-color: GREY; -fx-border-color: YELLOW;" text="SUBMIT" textFill="YELLOW" AnchorPane.rightAnchor="0.0" />
<Button id="cancelBtn" fx:id="a9" disable="true" layoutX="14.0" layoutY="271.0" minHeight="35.0" minWidth="150.0" mnemonicParsing="false" style="-fx-background-color: GREY; -fx-border-color: #ff9100;" text="CANCEL" textFill="#ff9100" AnchorPane.leftAnchor="0.0" />
</children>
</AnchorPane>
</content>
</TitledPane>
</children>
</VBox>
</children>
</HBox>
</children>
</AnchorPane>
GUI: the middle HNAVPanel should display a map which the Controller "RadarView()" loads and builds
Finally, the controller tries to load a map with a series of beacon on it. It worked propperly on its own, but when trying to add this "map" feature in the center column's AnchorPane "HNAVPanelPane" in my FXML GUI, it crashes raising the following exception:
Output:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:873)
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$1(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:750)
Caused by: java.lang.NullPointerException
at ground.view.RadarView.<init>(RadarView.java:37)
at build.Main.start(Main.java:23)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$4(WinApplication.java:185)
... 1 more
Exception running application build.Main
Java Result: 1
The controller code looks like this:
public class RadarView extends Pane{
@FXML
private AnchorPane HNAVPanelPane;
private IvyManager IM;
public RadarView(){
System.out.println("Initialized");
// Breaks after this point
// Its not appearing on HMI
HNAVPanelPane.getChildren().add(this);
// HNAV MAP
CartographyManagerXanthane cartography = new CartographyManagerXanthane();
Airspace myAirspace = new Airspace(cartography);
Map<String, IBeacon> beacons = myAirspace.getBeaconsByNameMap();
IBaseMap baseMap = cartography.loadBaseMap();
BaseMapView BMV = new BaseMapView(baseMap);
BeaconView BV = null;
this.getChildren().add(BMV);
SelfTraffic ST = new SelfTraffic(0, 0);
try{
IM = new IvyManager(ST);
}
catch(IvyException e){
throw new RuntimeException("Ivy bus had a failure...");
}
for(IBeacon beacon: beacons.values()){
BV = new BeaconView(beacon);
BMV.getChildren().add(BV);
}
SelfTrafficMarker aircraft = new SelfTrafficMarker(ST);
BMV.getChildren().add(aircraft.getMarker()); //discuss layers
panManager pM = new panManager(BMV);
dragManager dM = new dragManager(BMV);
zoomManager zM = new zoomManager(BMV);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
