How to check if textfield is empty?

随声附和 提交于 2019-12-10 12:54:38

问题


for my JavaFx application I want to check if the TextFields are empty, and if so, alert the user. These are the 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");

for now i just wanted to test the validation on one textfield. this is my validation function that is linked to the 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();
    }
}

But I get this error message as i press the 'Create Account' button:

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)

Thanks in advance!


回答1:


First of all, check if user_name is not null. If it is not, you are setting a null text value to the JavaFX component somewhere in your code.

if(user_name.getText().trim().isEmpty()){

In any case, applying the trim() to a null value is what causes your exception. You should not need this (see Uluk Biy's comment to this answer), but considering you created a user_name TextField, you must check first if the getText() method is not null.

Something like this should do the trick:

if (user_name.getText() == null || user_name.getText().trim().isEmpty()) {
     // your code here
}



回答2:


you can use Button disableProperty to bind with TextField. when the textfield is empty, then the button will be disabled, else it will be enabled.

use code mentioned below

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()
    //     )
    //  )

);



回答3:


Alright, this is how I did it and it works.

If in the event none of the above works, try the following:

Create a string variable, let's say String username; Next, try the following:

if(username.trim().isEmpty())

The original suggestion didn't work for me but creating the string did, so if the above suggestions did not work for you, give this a shot.




回答4:


This can be checked simply by

if(textField.getText().trim().equals(""))
{
system.out.println("textField is empty");
}



回答5:


textField.getText().trim().equals("")

Above statement worked like a charm for me.




回答6:


You can test this code replace the Name of my champs with the name of your 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");  }


来源:https://stackoverflow.com/questions/32866937/how-to-check-if-textfield-is-empty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!