Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException in JavaFX controller

Tags:

java

javafx

fxml

My project is really simple atm. It has two TextFields named latField and longField. All I'm trying to do is print the string values contained in these fields using the getText() method, and occasionally set them using the setText() method.

Note that I'm using javafx scene builder, and there is a FXMLDocument and FXMLDocumentController

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);
    }

}

I'm getting a Null Pointer Exception in the following line-

latField.setText("0.0");

Full Stacktrace-

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

Here is my 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);
    }

}

What I understand is that the controls defined in FXML are not getting injected into the FXMLDocumentController.java but don't know why. WHY?

like image 934
Lomesh Avatar asked Dec 15 '22 05:12

Lomesh


2 Answers

(Posted on behalf of the question author, to move it to the answer space).

This is now solved. The issue was with the id attribute in the fxml document.

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

If specified using the scene builder, it generates the id attribute by default. I noticed that in another working project, it was fx:id. Don't know why the scene builder is inconsistent.

Summary: use fx:id instead of id.

like image 191
3 revs, 2 users 86% Avatar answered Dec 17 '22 00:12

3 revs, 2 users 86%


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

For me this way doesn't work. You need to do it this way:

@FXML private  Button upButton;
...
@FXML private  Button  goButton;
like image 45
Towfik Alrazihi Avatar answered Dec 17 '22 01:12

Towfik Alrazihi