将参数从一个Controller传递到另一个Controller时,JavaFX错误“空指针异常”

时间:2018-04-17 15:56:35

标签: java javafx

我在youtube和google中尝试了所有解决方案但没有一个能够正常工作。问题是,当我想切换到新场景时,将数据发送到另一个场景,我收到错误

  

空指针异常

public class FirstController implements Initializable {

    @FXML private TextField textField;
    @FXML private Button btn;
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub
    }

    public void  onClick(ActionEvent event){
        //switching to new scene
        FXMLLoader loader=new FXMLLoader();
        loader.setLocation(getClass().getResource("/application/Second.fxml"));
        try{
            loader.load();
        }catch(Exception e){ }
        SecondController sn=loader.getController();
        sn.setText(textField.getText());
        Parent p=loader.getRoot();  
        Stage window=new Stage();
        window.setScene(new Scene(p));
        window.setTitle("dfd");
        window.show();
    }
}

main.fxml:

<AnchorPane prefHeight="450.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Main">
   <children>
      <Button fx:id="btn" layoutX="285.0" layoutY="264.0" mnemonicParsing="false" onAction="#onClick" text="Button" />
      <TextField fx:id="textField" layoutX="237.0" layoutY="213.0" />
   </children>
</AnchorPane>

第二控制器:

public class SecondController implements Initializable {

    @FXML private Label labelField;
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub  
    }

    public void setText(String name){
        this.labelField.setText(name);
    } 
}

Second.fxml:

`<AnchorPane prefHeight="450.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label fx:id="labelField" layoutX="297.0" layoutY="208.0" text="Label" />
   </children>

    </AnchorPane>

1 个答案:

答案 0 :(得分:0)

您的第二个FXML文件缺少fx:controller属性。因此,当您加载它时,没有控制器,因此loader.getController()返回null。

因此,当您调用sn.setText(...)时,会出现空指针异常。

只需将缺少的fx:controller属性添加到第二个FXML文件即可。假设SecondController包含在application包中,它将如下所示:

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

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Label?>

<AnchorPane prefHeight="450.0" prefWidth="650.0" fx:controller="application.SecondController" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label fx:id="labelField" layoutX="297.0" layoutY="208.0" text="Label" />
   </children>

</AnchorPane>

另请注意,使用

静默“压缩”异常是非常糟糕的做法
try {
    /* ... */
} catch (Exception e) { }

如果随后的代码依赖于try块中的代码成功,则会特别糟糕。特别是,在这种情况下,如果您对load()的调用导致异常(可能由于多种原因而发生,包括未找到FXML或无效,fx:id和字段名称,或者类型, 不匹配,处理程序的方法名称不匹配或具有错误的参数等),然后加载方法将无提示失败并且控制器将不在加载器中设置。这将再次导致sn为空。

以下是使用您的代码的完整示例,但修复了其他错误以及插入了缺少的fx:controller属性:

application/Main.java

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(FXMLLoader.load(getClass().getResource("First.fxml")));
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

application/First.fxml

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

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>

<AnchorPane prefHeight="450.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.FirstController">
   <children>
      <Button fx:id="btn" layoutX="285.0" layoutY="264.0" mnemonicParsing="false" onAction="#onClick" text="Button" />
      <TextField fx:id="textField" layoutX="237.0" layoutY="213.0" />
   </children>
</AnchorPane>

application/FirstController.java

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

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class FirstController implements Initializable {

    @FXML private TextField textField;
    @FXML private Button btn;
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub
    }

    public void  onClick(ActionEvent event) throws Exception {
        //switching to new scene
        FXMLLoader loader=new FXMLLoader();
        loader.setLocation(getClass().getResource("/application/Second.fxml"));
        loader.load();
        SecondController sn=loader.getController();
        sn.setText(textField.getText());
        Parent p=loader.getRoot();  
        Stage window=new Stage();
        window.setScene(new Scene(p));
        window.setTitle("dfd");
        window.show();
    }
}

application/Second.fxml

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

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Label?>

<AnchorPane prefHeight="450.0" prefWidth="650.0" fx:controller="application.SecondController" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label fx:id="labelField" layoutX="297.0" layoutY="208.0" text="Label" />
   </children>

</AnchorPane>

application/SecondController.java

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

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

public class SecondController implements Initializable {

    @FXML private Label labelField;
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub  
    }

    public void setText(String name){
        this.labelField.setText(name);
    } 
}