1

I am following ST's ST25SDK demo example and I am getting these errors:

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:498)
    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:498)
    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$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at com.st.myst25app.MainApp.initStage(MainApp.java:47)
    at com.st.myst25app.MainApp.start(MainApp.java:22)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    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$148(WinApplication.java:191)
    ... 1 more
Exception running application com.st.myst25app.MainApp

The error occurs at this line in the main app (MainApp.java line: 47) Which is saying it is caused by a java.lang.NullPointerException :

       mNfcTagController.setMainApp(this);

When I comment out this line, there's no errors but I need the controller to have access to the main app.

I am using a 32-bit jdk, which is required to run dll files and the version I am using is jdk1.8.0_144. I don't have any VM arguments.

Any ideas of what I may be missing or what I need to add to fix this issue?

Here is my main app shown below:

    package com.st.myst25app;
    import java.io.IOException;

    import com.st.myst25app.view.NfcTagController;
    import com.st.st25pc.model.readers.RFGenericReader;
    import com.st.st25pc.model.readers.st.STReader;
    import javafx.application.Application;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Scene;
    import javafx.scene.layout.AnchorPane;
    import javafx.stage.Stage;
    public class MainApp extends Application {
    private Stage mPrimaryStage;
    private RFGenericReader mActiveRFReader = null;
    private StringProperty mReaderStatus = new SimpleStringProperty();
@Override
public void start(Stage primaryStage) {
    mPrimaryStage = primaryStage;
    mPrimaryStage.setTitle("MyST25App");
    initStage();
    
    // Scan for USB reader and open device
    scanForReaders();
}
public static void main(String[] args) {
    launch(args);
}

/**
* Initializes the NfcTagView Stage
*/
public void initStage() {
try {
    // Load root layout from FXML file
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(MainApp.class.getResource("view/NfcTagView.fxml"));
    AnchorPane rootLayout = (AnchorPane) loader.load();

    // Show the scene containing the root layout
    Scene scene = new Scene(rootLayout);
    mPrimaryStage.setScene(scene);

    // Give the controller access to the main app
    NfcTagController mNfcTagController = loader.getController();
    mNfcTagController.setMainApp(this);
    
    // Display Stage on screen
    mPrimaryStage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }
        }
        /**
        * Scan USB ports for readers
        */

    public void scanForReaders() {
        // Try to instantiate a STReader (CR95HF, ST25R3911B-DISCO or ST25R3916-DISCO) to determine if one
        //is connected
        STReader stReader = new STReader();
        
        if (stReader.connect()) {
            // Now able to communicate with the reader
            mActiveRFReader = stReader;
        }

        if (mActiveRFReader != null) {
            setReaderStatus(stReader.getName() + " connected");
        } else {
            setReaderStatus("No reader is connected");
        }
    }
    public final StringProperty readerStatusProperty() {
        return mReaderStatus;
    }
    public final String getReaderStatus() {
        return readerStatusProperty().get();
    }
    public final void setReaderStatus(final String mReaderStatus) {
        readerStatusProperty().set(mReaderStatus);
    }
 }

Here is the Controller code:

    package com.st.myst25app.view;
    import com.st.myst25app.MainApp;
    /**
    * @author STMicroelectronics
    *
    */
    import javafx.fxml.FXML;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.Label;
    public class NfcTagController {
       @FXML
       private Button discoverButton;
       @FXML
       private Label tagUidLabel;
       @FXML
       private Label tagSizeLabel;
       @FXML
       private Label writeStatusLabel;
       @FXML
       private Label readerStatusLabel;
       // Reference to the main application
       private MainApp mainApp;
       /**
       * Initializes the controller class.
       * This method is automatically called after the fxml file is loaded.
       */
       @FXML
       private void initialize () {
            tagUidLabel.setText("No tag discovered");
            tagSizeLabel.setText("No tag discovered");
            writeStatusLabel.setText("");
            readerStatusLabel.setText("No reader is connected");
       }
       /**
       * setMainApp is called by the main application to give a reference of itself to the 
       controller.
       *
       * @param mainApp
       */
       public void setMainApp(MainApp mainApp) {
            this.mainApp = mainApp;
    
            //Bind reader connection status to a label to display the updated value whenever 
            there is a change
            readerStatusLabel.textProperty().bind(mainApp.readerStatusProperty());
       }
  }

FXML file:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<GridPane hgap="5.0" layoutX="12.0" layoutY="87.0" AnchorPane.bottomAnchor="10.0"
AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="284.0" minWidth="0.0" prefWidth="90.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="300.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button fx:id="discoverButton" mnemonicParsing="false" prefWidth="90.0" text="Discover Tag" />
<Label text="Detects Iso15693/NFC Forum Type 5 tag" GridPane.columnIndex="1" />
<Label text="Tag UID:" GridPane.halignment="RIGHT" GridPane.rowIndex="1" />
<Label fx:id="tagUidLabel" text="UID" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="EEPROM size:" GridPane.halignment="RIGHT" GridPane.rowIndex="2" />
<Label fx:id="tagSizeLabel" text="Size" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Button fx:id="writeNdefButton" mnemonicParsing="false" prefWidth="90.0" text="Write Uri"
GridPane.rowIndex="3" />
<Label fx:id="writeStatusLabel" text="Status" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label fx:id="readerStatusLabel" text="Reader Status" GridPane.columnIndex="1"
GridPane.halignment="RIGHT" GridPane.rowIndex="4" />
</children>
</GridPane>
</children>
</AnchorPane>
kook24
  • 11
  • 2
  • Post the stack trace as text, formatted as code. What does it actually say the problem is on that line? – James_D Jul 06 '22 at 17:18
  • In `NfcTagView.fxml`, what is the value of the `fx:controller` attribute? – trashgod Jul 06 '22 at 17:52
  • @trashgod I just added the fxml file – kook24 Jul 06 '22 at 18:40
  • 3
    You are missing the `fx:controller` attribute in the FXML file. If you look at the stack trace, the only possible way you can have a null pointer exception on that line is if `mNfcTagController` is null, which can only happen if the controller is not created by the `FXMLLoader`. So the obvious thing to look for is to check if the controller is properly configured in the FXML. – James_D Jul 06 '22 at 18:59

0 Answers0