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

java builds with different JDK versions : same artifacts?
Running the same JUnit test case multiple time with different data
LinkageError JAXB 2.0 -> 2.1 (Tomcat)
Strategies for migrating medium-sized code base from Java 1.4.2 to Java 5
Adding many frameworks to a Java application - will it have any negative side effects?
Why the awkward Design of System.out?
Quartz scheduler not displaying Log4j messages
Detect user logged on a computer using Java
How to relate ga:campaign and ga:pagePath (content) on Google Analytics Export API?
Java - Reg expression using groups
SoapAction Issue under Java 1.3
In java how do you refer to a class that is in the default package of a third party library?
How is this 13 bytes long?
How to turn off key handling on BasicTreeUI
Java EJB entity beans performance
Is there an standard class for Events in Java?

Categories

HOME
java
pdf
reflection
push-notification
reserved
dot
tesseract
programming-languages
c#-2.0
alpha
serverspec
facebook-messenger-bot
gitpitch
correlation
vb.net-2010
progressive-web-apps
saxon
serilog
text-rendering
alpine
amazonsellercentral
scriptcs
opencover
orleans
mps
graphicsmagick
mustache.php
tibco-mdm
intel-pin
phpfox
windows-error-reporting
libssl
madlib
frame
ioio
calibre
catch-all
normal-distribution
dartium
hot-module-replacement
service-discovery
webdriver-manager
email-templates
nomethoderror
estimote
netcdf4
babel-core
dds
logparser
integrity
media-player
iso8601
chord-diagram
deadbolt-2
gridpane
database-optimization
nsfilemanager
holder.js
dlna
tform
revolution-r
cloudbees
rtbkit
cartesian-product
kcachegrind
riak-cs
myo
sniffer
jsapi
truevault
bigbluebutton
offloading
java-metro-framework
mesa
htmlcleaner
flexmojos
xamlparseexception
mqx
html-editor
bitsharp
trusted
first-responder
appconkit
hgsubversion
mercurial-server
django-tagging
yslow
createwindow
gallio
bespin
iweb
genealogy

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