gpt4 book ai didi

java - 无法在 javafx 中将数组列表作为参数从一个窗口传递到另一个窗口

转载 作者:行者123 更新时间:2023-12-02 02:46:43 25 4
gpt4 key购买 nike

正在开发一个项目,我需要将数组列表作为参数传递到另一个窗口

1) 这是场景一

public void proceedNext(ActionEvent ae) throws Exception

{
al1.add(selectedData); // SelectedData is an ObservableList
System.out.println(al1);
Stage primaryStage = new Stage();
FXMLLoader loader = new FXMLLoader();
BorderPane root = loader.load(getClass().getResource("/Selection_Screen/SelectionScreen.fxml" ));

SelectionScreenController selectionController=(SelectionScreenController)loader.getController();


//am getting NullPointerException in this line
-> selectionController.getList(al1);//al1 is an arraylist and getList function is in another window


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

2) 这是场景 2,我想要来自场景一的数组列表

public ArrayList al;

@Override
public void initialize(URL location, ResourceBundle resources) {
//To change body of generated methods, choose Tools | Templates.
}

public void getList(ArrayList al)
{
this.al = al;

}

3) 这是SelectionScreen.fxml

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

<?import javafx.geometry.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import com.jfoenix.controls.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane prefHeight="700.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Selection_Screen.SelectionScreenController">
<center>
<JFXMasonryPane BorderPane.alignment="CENTER">
<children>
<JFXButton fx:id="editParameters1" buttonType="RAISED" onAction="#onClickEditParameters" prefHeight="115.0" prefWidth="239.0" ripplerFill="#e5afee" style="-fx-background-color: #d003f4;" text="Check Engine Details" textFill="#f8f5f5">
<font>
<Font size="21.0" />
</font>
</JFXButton>
<JFXButton fx:id="correlationModel" buttonType="RAISED" onAction="#onClickCorrelationModel" prefHeight="115.0" prefWidth="239.0" ripplerFill="#110be0" style="-fx-background-color: #ee1b1b;" text="Correlation Model" textFill="#f8f8f8">
<font>
<Font size="21.0" />
</font>
</JFXButton>
<JFXButton fx:id="addEngineDetails" buttonType="RAISED" onAction="#onClickAddEngineDetails" prefHeight="115.0" prefWidth="239.0" ripplerFill="#110be0" style="-fx-background-color: #0de029;" text="Add Engine Details" textFill="#fefffe">
<font>
<Font size="21.0" />
</font>
</JFXButton>
<JFXButton fx:id="editEngineDetails" buttonType="RAISED" onAction="#onClickEditEngineDetails" prefHeight="115.0" prefWidth="239.0" ripplerFill="#110be0db" style="-fx-background-color: #0b4ce2c4;" text="Edit Engine Details" textFill="#fffafa">
<font>
<Font size="21.0" />
</font>
</JFXButton>
<JFXButton fx:id="addParameters" buttonType="RAISED" onAction="#onClickAddParameters" prefHeight="115.0" prefWidth="239.0" ripplerFill="#110be0" style="-fx-background-color: #fff71e;" text="Add Parameters" textFill="#fcf8f8">
<font>
<Font size="21.0" />
</font>
</JFXButton>
<JFXButton fx:id="editParameters" buttonType="RAISED" onAction="#onClickEditParameters" prefHeight="115.0" prefWidth="239.0" ripplerFill="#110be0" style="-fx-background-color: #f88a04;" text="Edit Parameters" textFill="#f5f2f2">
<font>
<Font size="21.0" />
</font>
</JFXButton>
<JFXButton fx:id="trendAnalysis" buttonType="RAISED" onAction="#onClickTrendAnalysis" prefHeight="115.0" prefWidth="239.0" ripplerFill="#110be0" style="-fx-background-color: #0ba5e2;" text="Trend Analysis" textFill="#fcf9f9">
<font>
<Font size="21.0" />
</font>
</JFXButton>
<JFXButton fx:id="editParameters11" buttonType="RAISED" onAction="#onClickEditParameters" prefHeight="115.0" prefWidth="239.0" ripplerFill="#e5afee" style="-fx-background-color: #d003f4;" text="Check Engine Details" textFill="#f8f5f5">
<font>
<Font size="21.0" />
</font>
</JFXButton>
</children>
</JFXMasonryPane>
</center>
<top>
<Label text="Select any option from below" BorderPane.alignment="CENTER">
<font>
<Font size="32.0" />
</font>
</Label>
</top>
</BorderPane>

出现错误

java.lang.NullPointerException
at EngineSelection.EngineSelectionFXMLController.proceedNext(EngineSelectionFXMLController.java:105)
... 59 more

最佳答案

您正在使用静态方法 ( FXMLLoader.load(URL) ) 而不是实例方法加载 fxml。

这样 Controller ,根等。不存储在 FXMLLoader 中,并且它仍然包含初始 null 值。

修改加载 fxml 的代码来解决此问题:

// Pass fxml url in constructor instead
FXMLLoader loader = new FXMLLoader(getClass().getResource("/Selection_Screen/SelectionScreen.fxml"));

// make sure to use an instance method to load the fxml
BorderPane root = loader.load();

关于java - 无法在 javafx 中将数组列表作为参数从一个窗口传递到另一个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44463543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com