How to check an empty field?

for my JavaFx application. I want to check if the text fields are empty, and if so, warn the user. This is 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"); 

Now I just wanted to check the validation on a single text box. this is my check function related to 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 when I click 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!

+5
source share
6 answers

First of all, make sure user_name not null . If it is not, you set the text value to null for the JavaFX component somewhere in your code.

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

In any case, applying trim() to a null is the reason for your exception. You don't need to do this (see Uluk Biy 's comment on this answer), but given that you created a user_name text filter, first check if the getText() method is null .

Something like this should do the trick:

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

It can be checked simply

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

you can use Button disableProperty to bind to a TextField. when the text field is empty, the button will be disabled, otherwise it will be enabled.

use the code 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() // ) // ) ); 
0
source

Ok, that’s how I did it, and it works.

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

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

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

The original sentence did not work for me, but the line creation was done, so if the above sentences did not work for you, do it.

0
source

textField.getText (). Trim(). Equals ("")

The above expression worked like a charm for me.

0
source

You can test this code by replacing the name of my members 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"); } 
-1
source

Source: https://habr.com/ru/post/1232625/


All Articles