0

I'm currently writing a little Gui application with javaFx. I have a class App wich extends Application and wich is the controller of HauptFenster.fxml. This class builds up a Window with a menubar and a tabPane. Then I add the SplitPane from Uebersicht.fxml. The controller of Uebersicht.fxml is Uebersicht.java. Up to here everything working. But if I click a button which calls a Method, all of my fields in App are null. So all my fields get deleted by the JavaFX thread after the method "start" is called. You can see this with a debugger. Why is this happening? P.S: I removed all unnecessary code, so it shows only the Error, the Program does not make any practical Sense like it´s shown here.

App.Java:

    import java.io.IOException;
    import javafx.application.Application;
    import javafx.collections.ObservableList;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Scene;
    import javafx.scene.control.SplitPane;
    import javafx.scene.control.Tab;
    import javafx.scene.control.TabPane;
    import javafx.scene.layout.BorderPane;
    import javafx.stage.Stage;
    import view2.Uebersicht;

    public class App extends Application {

        private Stage primaryStage;
        private BorderPane rootLayout;
        private SplitPane uebersichtPane;
        private Uebersicht uebersicht;
        private ObservableList<Tab> tabs;


        public App() {

        }

        @Override
        public void start(Stage primaryStage) throws Exception {
            this.primaryStage = primaryStage;
            this.primaryStage.setTitle("FunktionsApp");

            initHauptfenster();
            initÜbersicht();
        }

        private void initHauptfenster() {
            try {
                // Load root layout from fxml file.
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(App.class.getResource("Hauptfenster.fxml"));
                rootLayout = (BorderPane) loader.load();

                tabs= ((TabPane)  rootLayout.getCenter()).getTabs();

                // Show the scene containing the root layout.
                Scene scene = new Scene(rootLayout);
                primaryStage.setScene(scene);
                primaryStage.show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        private void initÜbersicht() {
            try {
                // Load root layout from fxml file.
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(App.class.getResource("Uebersicht.fxml"));
                uebersichtPane= (SplitPane) loader.load();
                tabs.get(0).setContent(uebersichtPane);
                uebersicht = loader.getController();
                uebersicht.initText();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        @FXML 
        private void deleteFunktion() {
            uebersicht.initText();
        }
        @FXML
        public void speichern() {

            uebersicht.initText();
        }
        @FXML
        public void laden() {
            uebersicht.initText();
        }
        public static void main(String[] args) {
            launch(args);
        }
    }

Uebersicht.java

import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Label;
import view2.App;

public class Uebersicht {

    @FXML
    private Label lblStamm;
    @FXML
    private Label lblF;
    @FXML
    private Label lblF1;
    @FXML
    private Label lblF2;
    @FXML
    private Canvas canvas;


    public void initText() {
            lblStamm.setText("F(x)=");
            lblF.setText("f(x)=");
            lblF1.setText("f'(x)=");
            lblF2.setText("f''(x)=");
    }
}

HauptFenster.fxml:

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

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="630.0" prefWidth="820.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view2.App">
   <center>
      <TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
        <tabs>
          <Tab text="Übersicht" />
          <Tab text="Kurvendiskussion" />
            <Tab text="Eingabe" />
        </tabs>
      </TabPane>
   </center>
   <top>
      <MenuBar BorderPane.alignment="CENTER">
        <menus>
          <Menu mnemonicParsing="false" text="Datei">
            <items>
              <MenuItem mnemonicParsing="false" onAction="#speichern" text="Speichern" />
                  <MenuItem mnemonicParsing="false" onAction="#laden" text="Laden" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" onAction="#deleteFunktion" text="Funktion löschen" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
</BorderPane>

Uebersicht.fxml:

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

<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.canvas.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<SplitPane dividerPositions="0.7264367816091954" orientation="VERTICAL" prefHeight="437.0" prefWidth="552.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view2.Uebersicht">
  <items>
      <BorderPane>
         <center>
            <Canvas fx:id="canvas" BorderPane.alignment="CENTER" />
         </center>
      </BorderPane>
      <ScrollPane vbarPolicy="NEVER">
         <content>
            <BorderPane>
               <center>
                  <GridPane BorderPane.alignment="CENTER">
                    <columnConstraints>
                      <ColumnConstraints hgrow="SOMETIMES" percentWidth="100.0" />
                    </columnConstraints>
                    <rowConstraints>
                      <RowConstraints vgrow="NEVER" />
                      <RowConstraints minHeight="0.0" prefHeight="6.0" vgrow="NEVER" />
                      <RowConstraints vgrow="NEVER" />
                        <RowConstraints minHeight="0.0" prefHeight="6.0" vgrow="NEVER" />
                        <RowConstraints vgrow="NEVER" />
                        <RowConstraints minHeight="0.0" prefHeight="6.0" vgrow="NEVER" />
                        <RowConstraints vgrow="NEVER" />
                    </rowConstraints>
                     <children>
                        <Label fx:id="lblStamm" text="Label" wrapText="true">
                           <font>
                              <Font name="Times New Roman" size="25.0" />
                           </font>
                           <GridPane.margin>
                              <Insets bottom="2.0" />
                           </GridPane.margin>
                        </Label>
                        <Label fx:id="lblF" text="Label" wrapText="true" GridPane.rowIndex="2">
                           <font>
                              <Font name="Times New Roman" size="25.0" />
                           </font>
                           <GridPane.margin>
                              <Insets bottom="2.0" />
                           </GridPane.margin>
                        </Label>
                        <Label fx:id="lblF1" text="Label" wrapText="true" GridPane.rowIndex="4">
                           <font>
                              <Font name="Times New Roman" size="25.0" />
                           </font>
                           <GridPane.margin>
                              <Insets bottom="2.0" />
                           </GridPane.margin>
                        </Label>
                        <Label fx:id="lblF2" text="Label" wrapText="true" GridPane.rowIndex="6">
                           <font>
                              <Font name="Times New Roman" size="25.0" />
                           </font>
                           <GridPane.margin>
                              <Insets bottom="2.0" />
                           </GridPane.margin>
                        </Label>
                        <Separator prefWidth="200.0" GridPane.rowIndex="1" />
                        <Separator prefWidth="200.0" GridPane.rowIndex="3" />
                        <Separator prefWidth="200.0" GridPane.rowIndex="5" />
                     </children>
                     <padding>
                        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                     </padding>
                  </GridPane>
               </center>
            </BorderPane>
         </content>
      </ScrollPane>
  </items>
</SplitPane>
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • The instance of `App` you use as controller is a different one than the one JavaFX calls `start` for... Also you should probably learn about the `fx:id` attribute and the `@FXML` annotation. – fabian Apr 30 '18 at 11:25
  • Don't use the application class as the class for the controller. You only initialize the fields in the instance of `App` that is created when you call `Application.launch()` (the one on which `start()` is invoked): you don't initialize them in the instance that is created by the `FXMLLoader` when you load `HauptFenster.fxml` – James_D Apr 30 '18 at 11:25

0 Answers0