验证JFXTextField时出错

时间:2017-10-29 08:21:10

标签: javafx javafx-8

这是我的第一个JavaFX程序,我希望通过ePass接受eUser和密码的用户名。当我尝试使用validate检查文本字段是否为空时,我收到此错误:

    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$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: 
file:/Users/dylan/NetBeansProjects/JavaFXApplication2/dist/run350595374/JavaFXApplication2.jar!/javafxapplication2/FXMLDocument.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at javafxapplication2.JavaFXApplication2.start(JavaFXApplication2.java:23)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.NullPointerException
    at javafxapplication2.FXMLDocumentController.initialize(FXMLDocumentController.java:48)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 14 more
Exception running application javafxapplication2.JavaFXApplication2
Java Result: 1

当我从Controller.java文件中注释eUser.getValidators().add(vali);及其后面的行时,不会显示任何错误。

这是我的FXMLDocumentController.java文件:

public class FXMLDocumentController implements Initializable {
@FXML
private  JFXTextField eUser;
@FXML
private  JFXTextField ePass;

 @FXML
private void handleClose(MouseEvent event) {
    System.exit(0);
}


@Override
public void initialize(URL url, ResourceBundle rb) {
    RequiredFieldValidator vali = new RequiredFieldValidator();
    eUser.getValidators().add(vali);
    vali.setMessage("No Input Given");
    vali.focusedProperty().addListener(new ChangeListener<Boolean>(){
           public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
        if(!newValue)
        {
            eUser.validate();
        }
    }});}}

主要课程:

public class JavaFXApplication2 extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        stage.initStyle(StageStyle.UNDECORATED);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

1 个答案:

答案 0 :(得分:0)

也许这可能是问题所在。在FXML文件中,检查是否将这些ID设置为相关的文本字段。这可能是导致NullPointerException的原因。 如果您使用的是SceneBuilder,请单击相关的文本字段 - &gt;编码并设置那些textFields的fx:id。

顺便说一句,您可以使用PasswordField而不是TextField:)