提问者:小点点

JavaFX控制器中的NullPointerException


我的项目真的很简单。它有两个名为latFieldlongFieldgetText()方法打印这些字段中包含的字符串值,偶尔使用setText()[/code>方法设置它们。

请注意,我使用的是javafx场景构建器,并且有一个FXMLDocumentFXMLDocumentController

FXMLDocument.fxml

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

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

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" style="-fx-background-color: #eee;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pokemongocontroller.FXMLDocumentController">
    <children>
        <Label fx:id="label" layoutX="114.0" layoutY="92.0" minHeight="16" minWidth="69" />
        <Button id="upButton" layoutX="140.0" layoutY="84.0" mnemonicParsing="false" onAction="#upButtonClick" prefHeight="36.0" prefWidth="38.0" text="U" />
        <Button id="downButton" layoutX="140.0" layoutY="140.0" mnemonicParsing="false" onAction="#downButtonClick" prefHeight="36.0" prefWidth="38.0" text="D" />
        <Button id="leftButton" layoutX="94.0" layoutY="106.0" mnemonicParsing="false" onAction="#leftButtonClick" prefHeight="36.0" prefWidth="38.0" text="L" />
        <Button id="rightButton" layoutX="188.0" layoutY="106.0" mnemonicParsing="false" onAction="#rightButtonClick" prefHeight="36.0" prefWidth="38.0" text="R" />
        <TextField id="latField" layoutX="25.0" layoutY="27.0" prefHeight="22.0" prefWidth="69.0" text="0.0" />
        <TextField id="longField" layoutX="98.0" layoutY="27.0" prefHeight="22.0" prefWidth="69.0" text="0.0" />
        <Label layoutX="47.0" layoutY="49.0" text="Lat" />
        <Label layoutX="119.0" layoutY="49.0" text="Long" />
        <Button id="goButton" layoutX="188.0" layoutY="28.0" mnemonicParsing="false" onAction="#goButtonClick" prefHeight="28.0" prefWidth="78.0" text="GO!" />
      <TextField id="text" layoutX="47.0" layoutY="158.0" prefHeight="22.0" prefWidth="69.0" text="dfgdfg" />
    </children>
</AnchorPane>

FXMLDocumentController.java

public class FXMLDocumentController implements Initializable {

    private double delta=0.00001;
    private double latitude,longitude;


    @FXML
    private  Button upButton,downButton,rightButton,leftButton,goButton;
    private  TextField latField;
    private  TextField longField;

    @FXML
    private void upButtonClick(ActionEvent event) {
        System.out.println("You clicked U!");
        latitude+=delta;
        printCoordinates();

    }
    @FXML
    private void downButtonClick(ActionEvent event) {
        System.out.println("You clicked D!");
        latitude-=delta;
        printCoordinates();

    }
    @FXML
    private void leftButtonClick(ActionEvent event) {
        System.out.println("You clicked L!");
        longitude-=delta;
        printCoordinates();

    }
    @FXML
    private void rightButtonClick(ActionEvent event) {
        System.out.println("You clicked R!");
        longitude+=delta;
        printCoordinates();

    }
     @FXML
    private void goButtonClick(ActionEvent event) {
        System.out.println("You clicked Go!");
        latitude= Double.parseDouble(latField.getText());
        longitude= Double.parseDouble(longField.getText());
        printCoordinates();
        System.out.println(text.getText());

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        latField.setText("0.0");
        longField.setText("0.0");
        latitude= Double.parseDouble(latField.getText());
        longitude= Double.parseDouble(longField.getText());
        printCoordinates();
        System.out.println(text.getText());

    }    

    private void printCoordinates(){
         System.out.println("Latitude: "+latitude);
         System.out.println("Longitude: "+longitude);
    }

}

我在下面的行中得到一个空指针异常-

latField.setText("0.0");

全栈跟踪-

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
    at com.sun.javafx.application.LauncherImpl$$Lambda$50/1343441044.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
file:/E:/PokemonGo/PokemonGoController/dist/run583471250/PokemonGoController.jar!/pokemongocontroller/FXMLDocument.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2583)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
    at pokemongocontroller.PokemonGoController.start(PokemonGoController.java:26)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/107178178.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/355629945.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/1823905343.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1915503092.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
    ... 1 more
Caused by: java.lang.NullPointerException
    at pokemongocontroller.FXMLDocumentController.initialize(FXMLDocumentController.java:84)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    ... 22 more
Exception running application myapplication.ApplicationController
Java Result: 1

这是我的ApplcationController.java

import javafx.application.Application;
import javafx.event.EventHandler;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;

/**
 *
 * @author Lomesh
 */
public class ApplicationController extends Application {

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

我所理解的是,FXML中定义的控件没有被注入到FXMLDocumentController.java中,但不知道为什么。为什么?


共3个答案

匿名用户

(代表问题作者发布,以将其移动到答案空间)。

现在这个问题已经解决了。问题出在fxml文档中的id属性上。

<TextField id="latField" layoutX="25.0" layoutY="27.0" prefHeight="22.0" prefWidth="69.0" text="0.0" />

如果使用场景构建器指定,则默认情况下会生成 id 属性。我注意到在另一个工作项目中,它是fx:id。不知道为什么场景构建器不一致。

总结:使用fx: id代替id

匿名用户

@FXML private  Button upButton,downButton,rightButton,leftButton,goButton;

对我来说,这种方式是行不通的。您需要这样做:

@FXML private  Button upButton;
...
@FXML private  Button  goButton;

匿名用户

这是注入问题。确保控件的id和@FXML控件变量名相同。