java


Does the isEmpty() method check for null values? [duplicate]


This question already has an answer here:
What is a NullPointerException, and how do I fix it?
12 answers
I always see this in lots of code:
String s;
if (s == null || s.isEmpty()) {
//do something
}
but isn't just enough
String s;
if (s.isEmpty()) {
//do something
}
I am curious especially when I check JavaFX TextField emptiness.
In general,
s.isEmpty()
checks if s is an empty string, i.e. a string with no characters in it. If s is null, attempting to call s.isEmpty() (or attempting to call any other method on s) will throw a null pointer exception.
(Note that it doesn't really make sense to ask "Does the isEmpty() method check for null values": there's no way that s.isEmpty() can possibly check if s is null: the method cannot be invoked if s is null.)
In the specific case you cite: checking a text field for emptiness, note that TextField.getText() will not return null unless you explicitly set it to null first (with textField.setText(null)). In other words the default value is an empty string, and the text property will not be set to null by "internal" methods invoked as the user types. Since your text field should be confined to the immediate class in which it is defined (or the controller for the FXML file in which it is created), you can control calls to its setText(...) method and you can then guarantee that
TextField textField = ... ;
if (textField.getText() == null || textField.getText().isEmpty()) {
// ...
}
is equivalent to
TextField textField = ... ;
if (textField.getText().isEmpty()) {
// ...
}
by guaranteeing that textField.getText() == null is always false.

Related Links

can we change the gps values of a mobile device?
I have a 3 comboboxes that represent dob. The 3 comboboxes need to be next to one another like this dd mm yyyy. Is it possible using gridbag layout?
Install different Java JDK versions on Mountain Lion
sort arraylist of complex objects alphabetically
Changing font type within strings.xml
python pickle.dumps and struct.pack in Java
Java Hibernate constant in sql-query
methods and call by reference
Dynamically Generated Links in JSP not appearing
custom ListCellRenderer on Solaris (with jre5)
Playing large videos (FullHD+) with vlcj using DirectMediaPlayer
Java Inheritance - Getting a Parameter from Parent Class
Null params in method of web-service
Why is it not saying I won? [closed]
computing APY and using another method in java
Android Pre-Load Splash Screen

Categories

HOME
sendgrid
vbscript
proxy
comparison
raspberry-pi
dot
spring-cloud-stream
retrofit
fancybox
indesign
azure-storage-tables
swagger-ui
facebook-messenger-bot
primary-key
dacpac
vifm
commonmark
code-review
excel-vba-mac
autocad-plugin
ehcache
blazemeter
emgucv
sql-server-2012-express
preg-match
titanium-mobile
devextreme
email-templates
midl
flashair
thin
xml-documentation
ionicons
dropbox-php
yii2-extension
svmlight
media-player
jspdf-autotable
hittest
skobbler-maps
xml-attribute
bstr
qcustomplot
essence
whois
rhino-servicebus
unity-networking
teamcity-8.0
cannon.js
sniffer
mmc
gui-test-framework
asp.net-web-api-odata
socketexception
client-side-templating
xojo
uitouch
funcunit
rebol3
monomac
fmod
ivyde
tidy
coverflow
tws
batterylevel
eventlistener
android-hardware
multipage
labwindows
assembly-loading
eclipse-templates
winbugs14
telerik-scheduler
radcombobox
fixed-width
compiler-specific
substrings
webkit.net
libs
phonon
.net-1.0

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App