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

Unable to Save my Gallery bitmap into gallery
How to get permissions by name for each role a liferay user has
How to create a class and object in jsp?
How to merge 2 projects into 1 using Android studio?
Application or activity takes time to load some times
Java get image from webcam and send over socket
Spring Annotation Configuration in a library
(web crawler) how to get the text of news passages from news website
Get Android application package name during annotation processing
How can I send multiple strings from android Java to JNI?
Sprites array merges the when rendering sprites
Match a pattern and write the stream to a file using Java 8 Stream
How to create a PDF file from a JSP
multiple date format to change specifc format
String to Json object to Json Array in Java
How to efficiently wrap POJO with Bytebuddy?

Categories

HOME
ms-access
pandas
google-chrome-extension
coq
urbancode
isabelle
nullpointerexception
stock
adb
at-command
spring-cloud-stream
sqlite3
packages
webpack-2
cross-validation
basic
cloudkit
vifm
midi
numeral.js
flask-wtforms
django-admin
facebook-page
pugjs
flux
swiftlint
xlsxwriter
scriptcs
restful-authentication
emgucv
smb
user-interaction
quote
bluestacks
socialengine
bosh
scorm2004
google-qpx-express-api
avcapturesession
jmonkeyengine
multilingual
neuroscience
sharefile
email-templates
isbn
.net-4.6.2
wptoolkit
avconv
pango
dds
feeds
python-cryptography
webdriverjs
account-kit
blacklist
quartz-composer
eventkit
pycaffe
angular-strap
sts-springsourcetoolsuite
mikroc
packagist
apachebench
photobucket
historian
teamcity-8.0
cannon.js
spidermonkey
pundit
cakephp-3.1
graph-drawing
composite
sonarqube5.1.2
jsonpickle
gui-test-framework
id3v2
contenttype
mcts
floating-point-precision
aqtime
sitemesh
spring-io
dataservice
bulkloader
onsubmit
amazon-appstore
doh
xdomainrequest
sudzc
icanhaz.js
modelstate
compiler-specific
audio-capture
premature-optimization
noscript
commodore





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