javafx - How to check if textfield is empty? -


for javafx application want check if textfields empty, , if so, alert user. these 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 wanted test validation on 1 textfield. validation function linked 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 error message press 'create account' button:

exception in thread "javafx application thread"   java.lang.nullpointerexception     @ opdracht1.opdracht1.validation(opdracht1.java:36)     @ opdracht1.opdracht1$2$1.handle(opdracht1.java:103)     @ opdracht1.opdracht1$2$1.handle(opdracht1.java:98)     @ com.sun.javafx.event.compositeeventhandler.dispatchbubblingevent(compositeeventhandler.java:86)     @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(eventhandlermanager.java:238)     @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(eventhandlermanager.java:191)     @ com.sun.javafx.event.compositeeventdispatcher.dispatchbubblingevent(compositeeventdispatcher.java:59)     @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:58)     @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114)     @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56)     @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114)     @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56)     @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114)     @ com.sun.javafx.event.eventutil.fireeventimpl(eventutil.java:74)     @ com.sun.javafx.event.eventutil.fireevent(eventutil.java:49)     @ javafx.event.event.fireevent(event.java:198)     @ javafx.scene.node.fireevent(node.java:8411)     @ javafx.scene.control.button.fire(button.java:185)     @ com.sun.javafx.scene.control.behavior.buttonbehavior.mousereleased(buttonbehavior.java:182)     @ com.sun.javafx.scene.control.skin.behaviorskinbase$1.handle(behaviorskinbase.java:96)     @ com.sun.javafx.scene.control.skin.behaviorskinbase$1.handle(behaviorskinbase.java:89)     @ com.sun.javafx.event.compositeeventhandler$normaleventhandlerrecord.handlebubblingevent(compositeeventhandler.java:218)     @ com.sun.javafx.event.compositeeventhandler.dispatchbubblingevent(compositeeventhandler.java:80)     @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(eventhandlermanager.java:238)     @ com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(eventhandlermanager.java:191)     @ com.sun.javafx.event.compositeeventdispatcher.dispatchbubblingevent(compositeeventdispatcher.java:59)     @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:58)     @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114)     @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56)     @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114)     @ com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56)     @ com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114)     @ com.sun.javafx.event.eventutil.fireeventimpl(eventutil.java:74)     @ com.sun.javafx.event.eventutil.fireevent(eventutil.java:54)     @ javafx.event.event.fireevent(event.java:198)     @ javafx.scene.scene$mousehandler.process(scene.java:3757)     @ javafx.scene.scene$mousehandler.access$1500(scene.java:3485)     @ javafx.scene.scene.impl_processmouseevent(scene.java:1762)     @ javafx.scene.scene$scenepeerlistener.mouseevent(scene.java:2494)     @ com.sun.javafx.tk.quantum.glassvieweventhandler$mouseeventnotification.run(glassvieweventhandler.java:352)     @ com.sun.javafx.tk.quantum.glassvieweventhandler$mouseeventnotification.run(glassvieweventhandler.java:275)     @ java.security.accesscontroller.doprivileged(native method)     @ com.sun.javafx.tk.quantum.glassvieweventhandler.lambda$handlemouseevent$355(glassvieweventhandler.java:388)     @ com.sun.javafx.tk.quantum.quantumtoolkit.runwithoutrenderlock(quantumtoolkit.java:389)     @ com.sun.javafx.tk.quantum.glassvieweventhandler.handlemouseevent(glassvieweventhandler.java:387)     @ com.sun.glass.ui.view.handlemouseevent(view.java:555)     @ com.sun.glass.ui.view.notifymouse(view.java:937)     @ com.sun.glass.ui.win.winapplication._runloop(native method)     @ com.sun.glass.ui.win.winapplication.lambda$null$149(winapplication.java:191)     @ java.lang.thread.run(thread.java:745) 

thanks in advance!

first of all, check if user_name not null. if not, setting null text value javafx component somewhere in code.

if(user_name.gettext().trim().isempty()){

in case, applying trim() null value causes exception. should not need (see uluk biy's comment answer), considering created user_name textfield, must check first if gettext() method not null.

something should trick:

if (user_name.gettext() == null || user_name.gettext().trim().isempty()) {      // code here } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -