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

Inputs for jedis.incr(byte[] key)
Can't figure out how to have my markers re-open once the application has been restarted.
How to update configurations on a NETCONF enabled device?
Validate POST Request XML With POJO Created By JAXB Without XSD Or Marshal/Unmarshal
Spring Executable jar error on Windows
DynamicReport title band [closed]
JAX-WS throw java exception
Android plugin with .so lib's (UNITY)
MTOM enabled web service keeps the attachment in memory
Unable to store result of result set in variable [duplicate]
Not able to get Jsonstring from this link
Increase each node's value in a binary tree by 1
How to use SWIG to wrap a C struct ** for use in Java?
JSR 363 : formating a volume unit in decilitre
How to print a binary search tree populated with my own data type and its method
Substituting a character with a string from a string array in java

Categories

HOME
ms-access
client
variables
asp.net-core
wso2
urbancode
minimum-spanning-tree
push-notification
plone
react-virtualized
alfresco
programming-languages
filtering
spring-cloud-stream
spring-jdbc
sql-server-2016
qore
portia
paradox
append
communication
mapserver
apache-cayenne
saxon
xlsxwriter
tokenize
cloudhub
core-text
pass-by-reference
icloud-api
fish
facet
wpfdatagrid
tooltipster
spring-mybatis
strncpy
opentype
ejabberd-module
jaxb2
sql-server-2012-express
openoffice.org
mozilla
normal-distribution
http-digest
c11
sqlite2
angularjs-factory
bootstrapper
s
websphere-mq-fte
executenonquery
taffy
chain-builder
qsslsocket
multipeer-connectivity
pnotify
dandelion
veracode
brightcove
kbuild
magma
blacklist
migradoc
watchconnectivity
visual-c++-2008
elements
asp.net-4.5
skobbler-maps
blackberry-10
qgraphicsview
energy
execute
method-parameters
angular-local-storage
unity5.2.3
python-green
network-interface
mdt
wordpress-theme-customize
zend-route
eol
dml
phpthumb
rebol3
xceed-datagrid
kgdb
android-2.2-froyo
srs
mbr
ruby-datamapper
eventual-consistency
pushbackinputstream
plasma
ember-app-kit
factory-method
google-email-migration
javaspaces
objective-c-2.0
cufon
genshi
gdlib
gamma
backcolor
adrotator
eqatec
zend-decorators
windows-live-messenger
misv

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