【问题标题】:Unable to pass array list as parameter from one window to another in javafx无法在javafx中将数组列表作为参数从一个窗口传递到另一个窗口
【发布时间】:2017-06-09 17:22:51
【问题描述】:

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

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

【问题讨论】:

  • 你能发帖SelectionScreen.fxml吗?
  • 确定我已经编辑了问题请检查@James_D

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


【解决方案1】:

您正在使用静态方法 (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();

【讨论】:

  • 非常感谢@FABIAN,不知道怎么感谢你。你解决了我的大问题。再次感谢
  • @Awesh 如果它回答了你的问题,你应该mark the answer as correct
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多