This question already has an answer here:
javafx 8 compatibility issues - FXML static fields
                            
                                (1个答案)
                            
                    
                去年关闭。
        

    

我无法将JavaFX应用程序的数据从一个控制器类传输到另一个控制器类。我花了4天的时间来寻找解决方案,但没有成功。将不胜感激,因为我真的不知道下一步该怎么做。
我只是先尝试​​在标签中设置文本,然后再尝试从表的整个行中获取数据。

先感谢您。

更新

移动后
Parent root = fxmlLoader.load();

以上:
ManageUserController manageUserController = fxmlLoader.getController();
这对我来说很有意义,我仍然收到相同的NullPointerExcepetion,只是现在由以下条件触发:
manageUserController.setData("Test Text");

更新2

固定!基本上,这是因为在加载应用程序时已经初始化了每个场景,因此无法写入fxml文件。

// testController.java
package home;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class testcontroller implements Initializable {

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    public void switchscenes(MouseEvent mouseEvent) {
        try {

            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("manage-user.fxml"));
            Parent root = fxmlLoader.load();
            ManageUserController manageUserController = fxmlLoader.getController();

//
       manageUserController.userdetails(tblAllUsers.getSelectionModel().getSelectedItem().getFirstname());



     //////////// WHERE MY CODE BREAKS /////////
            manageUserController.setData("Test Text");
            System.err.println("before loading");



            //Method for testing purposes for moving between Scene
            Stage stage = new Stage();
            stage.setScene(new Scene(root));
            stage.setTitle("");
            stage.show();

//            Method for testing purposes for moving between Scene
//            Node node = (Node) mouseEvent.getSource();
//            Stage stage = (Stage)node.getScene().getWindow();
//            stage.close();
//
//            Scene scene = new Scene(FXMLLoader.load(getClass().getResource("Staff_Menu.fxml")));
//            stage.setScene(scene);
//            stage.show();

//            How I normally move between Scenes (Have a seperate library)
//        Scene_Library.SwitchToManageUser();
//        System.err.println("Transferring To User Manager Scene");


        } catch (IOException e) {
            e.printStackTrace();
        }

//        All ready Tried
//        String data = "Hello World!";

//        FXMLLoader loader = new FXMLLoader(getClass().getResource("manage-user.fxml"));
//        try {
//           loader.load();
//        } catch (IOException e) {
//            //e.printStackTrace();
//            System.err.println(e);
//
//        }
//        ManageUserController controller = new ManageUserController();
//        controller.setData(data);

//        Scene_Library.SwitchToManageUser();
//        System.err.println("Transferring To User Manager Scene");
    }
}



// ManageUserController.java
package home;

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

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

public class ManageUserController implements Initializable {
    private MemberObject currentMember;
    Logger logger = Logger.getLogger(getClass().getName());
    public Label lblUsernameID;
    @FXML
    public Label lblName;
    @FXML
    public Button btnUpdateUser;
    @FXML
    public Button btnUserBookings;
    @FXML
    public Button btnManageMembership;
    @FXML
    public Button btnDeleteMember;
    @FXML
    public Button btnBack;
    @FXML
    public Button btnSignOut;

    public void setCurrentMember(MemberObject currentMember){
        logger.log(Level.INFO, currentMember.firstname);
        logger.log(Level.INFO, currentMember.lastname);
        logger.log(Level.INFO, currentMember.email);
        this.currentMember = currentMember;
        this.lblUsernameID.setText((currentMember.getEmail()));
        this.lblName.setText(currentMember.getFirstname() + " " + currentMember.getLastname());
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}




//////// FXML File i'm trying to write data too ////////

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<AnchorPane fx:id="parent" style="-fx-background-color: transparent;" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="home.ManageUserController">
    <children>
        <HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #F05F5C;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
            <children>
                <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" HBox.hgrow="ALWAYS">
                    <children>
                        <Label text="Manage Member" textAlignment="CENTER" textFill="WHITE">
                            <font>
                                <Font size="24.0" />
                            </font>
                            <VBox.margin>
                                <Insets bottom="10.0" />
                            </VBox.margin>
                        </Label>
<!--                        <ImageView fitHeight="64.0" fitWidth="64.0" pickOnBounds="true" preserveRatio="true">0-->
<!--                            <image>-->
<!--                                <Image url="@../images/icons/logo/beach100.png" />-->
<!--                            </image>-->
<!--                        </ImageView>-->
                        <HBox style="-fx-border-color: white; -fx-background-radius: 0;">
                            <VBox.margin>
                                <Insets left="50.0" right="50.0" top="10.0" />
                            </VBox.margin>
                            <children>
                        <Label fx:id="lblUsernameID" prefHeight="31.0" prefWidth="206.0" text="Username ID" textFill="WHITE" HBox.hgrow="ALWAYS">
                           <padding>
                              <Insets left="10.0" />
                           </padding>
                        </Label>
                                <Button mnemonicParsing="false" prefHeight="31.0" style="-fx-background-color: transparent;" HBox.hgrow="ALWAYS">
                                    <graphic>
                                        <ImageView fitHeight="24.0" fitWidth="24.0" pickOnBounds="true" preserveRatio="true">
                                            <image>
                                                <Image url="@../images/icons/accnt/account.png" />
                                            </image>
                                        </ImageView>
                                    </graphic>
                                </Button>
                            </children>
                        </HBox>
                        <HBox style="-fx-border-color: white; -fx-background-radius: 0;">
                            <children>
                        <Label fx:id="lblName" prefHeight="31.0" prefWidth="206.0" text="First and Last Name" textFill="WHITE" HBox.hgrow="ALWAYS">
                           <padding>
                              <Insets left="10.0" />
                           </padding>
                        </Label>
                                <Button mnemonicParsing="false" prefHeight="31.0" style="-fx-background-color: transparent;" HBox.hgrow="ALWAYS">
                                    <graphic>
                                        <ImageView fitHeight="24.0" fitWidth="24.0" pickOnBounds="true" preserveRatio="true">
                                            <image>
                                                <Image url="@../images/icons/mrmbership/user.png" />
                                            </image>
                                        </ImageView>
                                    </graphic>
                                </Button>
                            </children>
                            <VBox.margin>
                                <Insets left="50.0" right="50.0" top="10.0" />
                            </VBox.margin>
                        </HBox>
                    </children>
                </VBox>
                <VBox alignment="CENTER" prefWidth="250.0" style="-fx-background-color: #26283c;">
                    <children>
                        <HBox alignment="CENTER" style="-fx-background-color: #2b2a4c;">
                            <VBox.margin>
                                <Insets left="20.0" right="20.0" top="20.0" />
                            </VBox.margin>
                            <padding>
                                <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                            </padding>
                            <children>
                                <Button fx:id="btnUpdateUser" mnemonicParsing="false" prefHeight="25.0" prefWidth="196.0" style="-fx-background-color: transparent; -fx-text-fill: white;" text="Update Details" HBox.hgrow="ALWAYS" />
                                <ImageView fitHeight="24.0" fitWidth="24.0" pickOnBounds="true" preserveRatio="true">
                                    <image>
                                        <Image url="@../images/icons/general/baseline_settings_white_18dp.png" />
                                    </image>
                                </ImageView>
                            </children>
                        </HBox>
                        <HBox alignment="CENTER" style="-fx-background-color: #2b2a4c;">
                            <padding>
                                <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                            </padding>
                            <children>
                                <Button fx:id="btnUserBookings" mnemonicParsing="false" prefHeight="25.0" prefWidth="196.0" style="-fx-background-color: transparent; -fx-text-fill: white;" text="Bookings" HBox.hgrow="ALWAYS" />
                                <ImageView fitHeight="24.0" fitWidth="24.0" pickOnBounds="true" preserveRatio="true">
                                    <image>
                                        <Image url="@../images/icons/mrmbership/user.png" />
                                    </image>
                                </ImageView>
                            </children>
                            <VBox.margin>
                                <Insets left="20.0" right="20.0" top="10.0" />
                            </VBox.margin>
                        </HBox>
                        <HBox alignment="CENTER" style="-fx-background-color: #2b2a4c;">
                            <padding>
                                <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                            </padding>
                            <children>
                                <Button fx:id="btnManageMembership" mnemonicParsing="false" prefHeight="25.0" prefWidth="196.0" style="-fx-background-color: transparent; -fx-text-fill: white;" text="Manage Membership" HBox.hgrow="ALWAYS" />
                                <ImageView fitHeight="24.0" fitWidth="24.0" pickOnBounds="true" preserveRatio="true">
                                    <image>
                                        <Image url="@../images/icons/mrmbership/usergold.png" />
                                    </image>
                                </ImageView>
                            </children>
                            <VBox.margin>
                                <Insets left="20.0" right="20.0" top="10.0" />
                            </VBox.margin>
                        </HBox>
                  <HBox alignment="CENTER" style="-fx-background-color: #2b2a4c;">
                     <padding>
                        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                     </padding>
                     <children>
                        <Button fx:id="btnDeleteMember" mnemonicParsing="false" prefHeight="25.0" prefWidth="196.0" style="-fx-background-color: transparent; -fx-text-fill: white;" text="Detele Member" HBox.hgrow="ALWAYS" />
                        <ImageView fitHeight="24.0" fitWidth="24.0" pickOnBounds="true" preserveRatio="true">
                           <image>
                              <Image url="@../images/icons/general/baseline_close_white_18dp.png" />
                           </image>
                        </ImageView>
                     </children>
                     <VBox.margin>
                        <Insets left="20.0" right="20.0" top="10.0" />
                     </VBox.margin>
                  </HBox>
                  <HBox alignment="CENTER">
                     <padding>
                        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                     </padding>
                     <children>
                        <Button fx:id="btnBack" mnemonicParsing="false" prefHeight="32.0" prefWidth="140.0" style="-fx-background-color: transparent; -fx-text-fill: white;" text="Back" HBox.hgrow="ALWAYS">
                           <HBox.margin>
                              <Insets left="5.0" />
                           </HBox.margin>
                        </Button>
                        <Button fx:id="btnSignOut" mnemonicParsing="false" prefHeight="32.0" prefWidth="140.0" style="-fx-background-color: transparent; -fx-text-fill: white;" text="Sign Out" />
                     </children>
                     <VBox.margin>
                        <Insets left="20.0" right="20.0" top="10.0" />
                     </VBox.margin>
                  </HBox>
                    </children>
                    <HBox.margin>
                        <Insets />
                    </HBox.margin>
                </VBox>
            </children>
        </HBox>
    </children>
    <opaqueInsets>
        <Insets />
    </opaqueInsets>
</AnchorPane>







// MY ERROR LOG

/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=60327:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/lib/tools.jar:/Users/jack/Dropbox/University/Coventry/year-2/260CT/Dev/Proto/out/production/Proto:/Users/jack/Documents/db-derby-10.14.2.0-bin/lib/derby.jar:/Users/jack/Documents/db-derby-10.14.2.0-bin/lib/derbyclient.jar:/Users/jack/Documents/db-derby-10.14.2.0-bin/lib/derbynet.jar:/Users/jack/Documents/db-derby-10.14.2.0-bin/lib/derbytools.jar:/Users/jack/Documents/mysql-connector-java-8.0.15.jar home.Main
Login Successful
Transferred to Home Page
Transferring to New test table
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$358(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
Caused by: 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 sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 28 more
Caused by: java.lang.NullPointerException
    at home.ManageUserController.setData(ManageUserController.java:30)
    at home.testcontroller.switchscenes(testcontroller.java:94)
    ... 38 more

最佳答案

您需要先加载才能获得控制器。

所以叫这个,

Parent root = fxmlLoader.load();


在您打电话之前,

ManageUserController manageUserController = fxmlLoader.getController();

10-07 23:23