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

时间:2017-06-09 17:22:52

标签: java arraylist javafx javafx-2 javafx-8

我正在处理一个项目,我需要将数组列表作为参数

传递给另一个窗口

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

1 个答案:

答案 0 :(得分:2)

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

这种方式控制器,根等。未存储在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();