1

i am developing an Javafx application with Spring Security enabled in it. then, i have created a fxml form which have a textField with id equals to txtField. later i anottated a method with @Secured Anottation, but when i try to set text to textField i get a NullPointerException.

Controller Class is as follow:



    @Controller
    public class ControladorInicio {
        @FXML
        private ResourceBundle resources;
        @FXML
        private URL location;
        @FXML
        public TextField textField;

        @FXML
        @Secured("ROLE_OFERTA_ADMIN")
        public void onButton3(ActionEvent event) {
            System.out.println("has permission");
        }

        @FXML
        public void initialize() {
            textField.setText("This is a text"); //I get a null pointer exception here
        }
    }

i suspect javafx is not injecting properties fields to the controller when i use @Secured. but, when i comment the @Secured anottation all is fine.

kato2
  • 535
  • 4
  • 15
  • have you tried making the fields public? FXMLLoader has to make stuff accessible through reflection and maybe this is what is failing? – tomsontom Jul 16 '13 at 14:01
  • sorry this has to be with aop self invocation issue. This is not an JavaFX bug. sorry newly but when i made the question, i didn't know about this bug. – kato2 Aug 12 '13 at 17:29

3 Answers3

3

In the fxml, make sure you set the field "fx:id". The field "id" seems to be the CSS id.

jamie
  • 580
  • 5
  • 16
0

If on FXML your textfield id equal to txtField , when you want use it youn need to write

@FXML
private TextField txtField;

with the same name as your fxml id. Or it will not works.

agonist_
  • 4,890
  • 6
  • 32
  • 55
-1

The method needs the right signature.

@FXML
    public void initialize(javafx.event.ActionEvent evt) {
        textField.setText("This is a text"); //I get a null pointer exception here
    }
msj121
  • 2,812
  • 3
  • 28
  • 55
  • No, that would not be the issue here. `initialize()` is the right signature for FXML controller initialization logic and the FXMLLoader will detect the method and invoke it on the controller when the FXML file is loaded. The ActionEvent parameter is necessary for action event handlers, but that is unrelated to whether or not the textField itself was correctly injected into the controller, which is what the question is querying about. – jewelsea May 03 '17 at 18:30