java.lang.NullPointerException用于处理Javafx中另一个FXML内的FXML

时间:2015-02-07 03:30:28

标签: nullpointerexception javafx fxml

我一直试图弄清楚如何在另一个FXML文件中处理FXML文件。但我有一个例外。 :( 这是我的内部FXML(视图包中的NewInside.fxml):

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane prefHeight="305.0" prefWidth="360.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.NewInsideController">
   <children>
      <Button fx:id="newBtn" layoutX="30.0" layoutY="202.0" mnemonicParsing="false" onAction="#btnClick" text="Button" />
      <TextField fx:id="newTxt" layoutX="30.0" layoutY="134.0" prefHeight="25.0" prefWidth="280.0" />
      <Label fx:id="newLbl" layoutX="30.0" layoutY="62.0" prefHeight="17.0" prefWidth="280.0" />
   </children>
</AnchorPane>

这是它的控制器(控制器包中的NewInsideController.java):

package controller;

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

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class NewInsideController implements Initializable{

    @FXML public static Label newLbl;
    @FXML public static TextField newTxt;
    @FXML public static Button newBtn;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    @FXML
    private void btnClick(ActionEvent e){
        System.out.println("Button is clicked!");
        newLbl.setText(newTxt.getText());
    }

}

这是外部FXML(视图包中的New.fxml):

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane prefHeight="380.0" prefWidth="529.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.NewController">
   <children>
      <fx:include source="NewInside.fxml" />
   </children>
</AnchorPane>

这是它的控制器(控制器包中的NewController.java):

package controller;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;

public class NewController implements Initializable {

    /*@FXML private Label newLbl;
    @FXML private Button newBtn;
    @FXML private TextField newTxt;
    */

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    /*@FXML
    public void btnClick(ActionEvent e){
        newLbl.setText(newTxt.getText());
    }*/

}

最后,这是应用程序包中的Main.java:

package application;

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


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/view/New.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

当我点击按钮时,它给了我很长的例外: &#34;线程中的异常&#34; JavaFX应用程序线程&#34; java.lang.RuntimeException:java.lang.reflect.InvocationTargetException .... ..... 引起:java.lang.NullPointerException     在controller.NewInsideController.btnClick(NewInsideController.java:27)     ......还有57个&#34;

请帮帮我! :(

1 个答案:

答案 0 :(得分:1)

正如@Inge建议的那样,使用静态字段不是正确的方法。另外,您应该使用public而不是private。如果您需要公开控件,请使用它们的属性。

你可以在这里阅读原因:

只需在NewInsideController

中更改此设置即可
@FXML private Label newLbl;
@FXML private TextField newTxt;
@FXML private Button newBtn;

修改

如果您想要从其他类访问控件,而不是添加getter / setter来直接公开控件,最好只公开它们的属性。

例如,要访问这些控件的文本,您可以添加:

public StringProperty newLblText() { 
    return newLbl.textProperty();
}
public StringProperty newTxtText() { 
    return newTxt.textProperty();
}
public StringProperty newBtnText() { 
    return newBtn.textProperty();
}

这将允许您绑定(或收听更改),从控制器外部获取/设置任何文本属性。

要从NewInsideController获取NewController的有效实例,请按此link。首先修改New.fxml

<fx:include fx:id="newInside" source="NewInside.fxml" />

现在您可以获取一个实例,并添加一些具有这些属性的侦听器:

public class NewController implements Initializable {

    @FXML
    private Parent newInside;
    @FXML
    private NewInsideController newInsideController;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        newInsideController.newBtnText().addListener(
            (obs,s,s1)->System.out.println("nweBtn Text;" +s1));
        if(newInsideController.newLblText().get().isEmpty()){
            newInsideController.newLblText().set("Text for the textfield");
        }
        newInsideController.newTxtText().addListener(
           (obs,s,s1)->System.out.println("s1 "+s1));
    }    
}