无法将数组列表作为参数从一个 window 传递到 javafx 中的另一个
Unable to pass array list as parameter from one window to another in javafx
我正在做一个项目,我需要将数组列表作为参数传递给另一个 window
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。
这样控制器,root 等。未存储在 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();
我正在做一个项目,我需要将数组列表作为参数传递给另一个 window
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。
这样控制器,root 等。未存储在 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();