对于我的JavaFx应用程序我想检查TextFields是否为空,如果是,则提醒用户。 这些是TextFields:
VBox fields = new VBox();
Text usernametext = new Text("User name");
TextField user_name = new TextField();
Text firstnametext = new Text("First name");
TextField first_name = new TextField();
Text lastnametext = new Text("Last name");
TextField last_name = new TextField();
Text ibantext = new Text("IBAN");
TextField iban = new TextField();
Text passwordtext = new Text("Password");
TextField password = new TextField();
Text confirmpasstext = new Text("Confirm password");
TextField confirmpass = new TextField();
Button createBtn = new Button("Create account");
现在我只想在一个文本字段上测试验证。这是我的验证函数,它链接到createBtn:
public void validation(){
if(user_name.getText().trim().isEmpty()){
Alert fail= new Alert(AlertType.INFORMATION);
fail.setHeaderText("failure");
fail.setContentText("you havent typed something");
fail.showAndWait();
} else {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setHeaderText("Succes");
alert.setContentText("Account succesfully created!");
alert.showAndWait();
}
}
但是当我按下“创建帐户”时,我收到此错误消息。按钮:
Exception in thread "JavaFX Application Thread"
java.lang.NullPointerException
at opdracht1.Opdracht1.validation(Opdracht1.java:36)
at opdracht1.Opdracht1$2$1.handle(Opdracht1.java:103)
at opdracht1.Opdracht1$2$1.handle(Opdracht1.java:98)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
提前致谢!
答案 0 :(得分:8)
首先,检查user_name
是否 null 。如果不是,则在代码中的某处为JavaFX组件设置 null 文本值。
if(user_name.getText().trim().isEmpty()){
在任何情况下,将trim()
应用于 null 值都会导致您的异常。您不应该这样做(请参阅 Uluk Biy 对此答案的评论),但考虑到您创建了user_name
TextField,您必须先检查getText()
方法不是 null 。
这样的事情可以解决问题:
if (user_name.getText() == null || user_name.getText().trim().isEmpty()) {
// your code here
}
答案 1 :(得分:1)
您可以使用Button disableProperty与TextField绑定。当textfield为空时,该按钮将被禁用,否则将被启用。
使用下面提到的代码
createBtn.disableProperty().bind(
Bindings.createBooleanBinding( () ->
user_name.getText().trim().isEmpty(), user_name.textProperty()
)
// If you want to check more text field, use it as by removing comments
//.or( Bindings.createBooleanBinding(
// first_name.getText.trim().isEmpty(), first_name.textProperty()
// )
// )
);
答案 2 :(得分:1)
好吧,这就是我做到的方式而且有效。
如果上述情况均无效,请尝试以下操作:
创建一个字符串变量,让我们说String username; 接下来,尝试以下操作:
if(username.trim().isEmpty())
原始建议对我没有用,但创建了字符串,所以如果上述建议对你不起作用,请给它一个镜头。
答案 3 :(得分:0)
这可以通过
简单地检查if(textField.getText().trim().equals(""))
{
system.out.println("textField is empty");
}
答案 4 :(得分:0)
textField.getText()修剪()等于("&#34)。
上面的陈述对我来说就像是一种魅力。
答案 5 :(得分:-1)
您可以测试此代码,使用TextFeild的名称替换我的冠军姓名
if ((nom_pt.getText().toString().compareTo("") == 0) ||
(prenom_pt.getText().toString().compareTo("") == 0) ||
(cin_pt.getText().toString().compareTo("") == 0) ||
(gsm_pt.getText().toString().compareTo("") == 0)) {
JOptionPane.showMessageDialog(null, "champs vide");
} else { JOptionPane.showMessageDialog(null, "Enregistrement a été ajouter"); }