Tanner Summers
Tanner Summers

Reputation: 663

JavaFX 8 FXML TextField (maybe other variables) are always null

I am practicing a small chat box window and I try to make it so the user must enter a name into the textfield box (a username) but I been having problems and I am sure this will not be the first time. I keep getting nullpointerexception every time I try to use one of the variables, for right now, it is textfield. So I initialize it but then it is always blank string even when I type in a username.

app.java

public class app extends Application {

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXMLApp.fxml"));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
}

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

}

FXMLApp.fxml (deleted what was not needed)

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

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

<AnchorPane id="AnchorPane" maxHeight="900.0" maxWidth="800.0" minHeight="300.0" minWidth="200.0" prefHeight="600.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="chatappclient.FXMLChatAppClientController">
    <children>
      <TextField fx:id="username" layoutX="176.0" layoutY="488.0" promptText="enter username" />
      <Button fx:id="login" layoutX="176.0" layoutY="519.0" mnemonicParsing="false" onAction="#login" text="Login" />
    </children>
</AnchorPane>

as you can see the login button calls

onAction="#login"

last file: FXMLAppController

    package chatappclient;

    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.application.Platform;
    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.TextArea;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.Pane;
    import javax.swing.JOptionPane;

    public class FXMLAppController implements Initializable 
    {
        @FXML
        private Pane top_container;
        private Pane bottom_container;
        private TextField username ;
        private String name;
        private Button login;

        public FXMLChatAppClientController ()
        {
           // without this, I get nullpointerexception
            // with it, the textfield is always null
            username = new TextField();
        }

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


        @FXML
        private void login() 
        {
            // this returns empty string, if I did not initialize it, it would be an exception
            System.out.println("DEBUG: " + username.getText());
        }
    }

Upvotes: 0

Views: 2082

Answers (1)

James_D
James_D

Reputation: 209330

You need to annotate each of the fields defined in the FXML file with @FXML:

private Pane top_container;
private Pane bottom_container;
@FXML
private TextField username ;
private String name;
@FXML
private Button login;

Upvotes: 1

Related Questions