处理2个控制器之间的控件时的java.lang.NullPointerException - Javafx

时间:2015-02-08 02:58:48

标签: javafx nullpointerexception controller fxml

我一直在Javafx的两个场景之间尝试按钮事件。

这是Start.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="292.0" prefWidth="383.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.StartController">
   <children>
      <Label fx:id="lbl1" layoutX="87.0" layoutY="60.0" prefHeight="17.0" prefWidth="209.0" />
      <TextField fx:id="txt1" layoutX="87.0" layoutY="121.0" prefHeight="25.0" prefWidth="209.0" />
      <Button fx:id="btn1" layoutX="171.0" layoutY="191.0" mnemonicParsing="false" onAction="#btn1Click" text="Click" />
   </children>
</AnchorPane>

这是Preserences.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="292.0" prefWidth="383.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.PreferencesController">
   <children>
      <Label fx:id="lbl2" layoutX="87.0" layoutY="60.0" prefHeight="17.0" prefWidth="209.0" />
      <TextField fx:id="txt2" layoutX="87.0" layoutY="121.0" prefHeight="25.0" prefWidth="209.0" />
      <Button fx:id="btn2" layoutX="171.0" layoutY="191.0" mnemonicParsing="false" onAction="#btn2Click" text="Click" />
   </children>
</AnchorPane>

这是Main.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="481.0" prefWidth="468.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.MainController">
   <children>
      <TabPane layoutX="14.0" layoutY="14.0" prefHeight="481.0" prefWidth="468.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <tabs>
          <Tab closable="false" text="Start">
               <content>
                  <fx:include source="Start.fxml" />
               </content>
          </Tab>
          <Tab closable="false" text="Preferences">
               <content>
                  <fx:include source="Preferences.fxml" />
               </content>
          </Tab>
        </tabs>
      </TabPane>
   </children>
</AnchorPane>

这是StartController.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 StartController implements Initializable {

    @FXML public static Label lbl1;
    @FXML public static TextField txt1;
    @FXML public static Button btn1;

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

    }

    @FXML private void btn1Click(ActionEvent e){
        System.out.println("Button 1 clicked");
        lbl1.setText(PreferencesController.txt2.getText());
    }

}

这是PreferencesController.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 PreferencesController implements Initializable {

    @FXML public static Label lbl2;
    @FXML public static TextField txt2;
    @FXML public static Button btn2;

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

    }

    @FXML private void btn2Click(ActionEvent e){
        System.out.println("Button 2 clicked");
        lbl2.setText(StartController.txt1.getText());
    }

}

这是MainController.java(在控制器包中):

package controller;

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

import javafx.fxml.Initializable;

public class MainController implements Initializable{

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {

    }

}

最后,这是应用程序包中的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/Main.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);
    }
}

但是当我点击按钮时,它会给我错误:

"Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException...
...
Caused by: java.lang.NullPointerException
    at controller.StartController.btn1Click(StartController.java:26)
    ... 61 more"

请帮帮我! :(

1 个答案:

答案 0 :(得分:2)

好的,所以NPE正在这条线上:

    lbl1.setText(PreferencesController.txt2.getText());

在基本的Java级别,只有两种方法可以实现这一点:

    lnl1null或时,
  • PreferencesController.txt2null时。

鉴于PreferencesController.txt2是一个静态字段,没有其他解释是可能的。


我不熟悉JavaFX或FXML,但我怀疑你的错误是将字段声明为static。我怀疑这会导致FXML机制无法正确地向UI组件注入引用。因此,这些变量保持为null ...并且您获得了NPE。

确认在这里:https://stackoverflow.com/a/23109125/139985。 (感谢@James_D链接)