Reputation: 233
I am trying to filter this exception by checking whether the HashMap is Null using an if statement. If the HashMap is null then running a method which loads it with values however seems that the if statement is also giving an NullPointerException.
How can this be restructured?
sqldata
is the Hashmap and it is being loaded elsewhere however there is a possibility that it might not have any values (as per design) in which case I am trying to check (if the HashMap is empty/null) and reload it using a code snippet below:
if (sqldata.isEmpty()||sqldata.equals(null)) {
sqldata = fileloader.dbhealthload();
}
Upvotes: 1
Views: 2434
Reputation: 6423
In Java null
is not an object, just a void reference, so you shouldn't call .equals
on variable that can be null. The only correct way is to compare it with reference comparison:
sqldata == null
Another thing is ||
is lazy, this means as soon as it finds first (from left to right) true value, it does not evaluate other operands. Hence a very common pattern:
sqldata == null || sqldata.isEmpty()
And you should use it. The operator &&
is lazy also, so things like:
sqldata != null && sqldata.doStuff()
are possible.
The same applies to any variable, for example String variable.
s == null || s.isEmpty()
When it comes to comparing strings with a literal another common pattern should be used:
"a value".equals( s )
It work's and is safe because:
s
is null .equals can handle it, just nothing equals to nullAs a guideline, don't compare strings the other way around:
s.equals("a value")
and don't use reference comparison:
s == "a value"
A string variable sometimes can point to the same object as the literal, but not always!
Upvotes: 1
Reputation: 4639
Generally NullPointerException
throw when when you try to use a reference that points to no location in memory (null).Calling a method on a null
reference or trying to access a field of a null reference will trigger a NullPointerExecption
.
In your code you are try to calling method isEmpty()
using null reference of you HashMap object.
Always check null before checking any other condition or accessing method using object reference.
For Example :
HashMap sqldata = new HashMap();
if(sqldata!=null && sqldata.isEmpty())
{
sqldata = fileloader.dbhealthload();
}
Upvotes: 0
Reputation: 2633
The reason for null pointer exception might be that you HashMap is never instantiated. One way to correct this is to ensure that the HashMap is always instantiated and then restructuring the code in the following way:
if(!sqldata.isEmpty()) {
do operations ...
} else {
reload ...
}
It is recommended to use the above programming practice. However, if you do not want an enforced instantiation of the HashMap object, then you need to check that is it null or not and once you have checked that, you should proceed to check whether it is empty. The code in that case will be:
if(!(sqldata == null)) {
if(!sqldata.isEmpty()) {
do operations ...
}
} else {
reload or instantiate ...
then do operations ...
}
You may also write the other way around.
Upvotes: 1
Reputation: 1176
sqldata.equals
is running the method equals
of sqldata. In the case that sqldata is null, it does not have any methods, so java does not find it and throws a NullPointerException. You should check it with ==
:
if (sqldata == null || sqldata.isEmpty()) {
sqldata = fileloader.dbhealthload();
}
Upvotes: 1
Reputation: 1546
First you need to check whether sqldata is null or not then you can proceed to check whether HashMap
is empty. Follow the given approach.
if (sqldata==null||sqldata.isEmpty())
{
sqldata = fileloader.dbhealthload();
}
if sqldata
is null
and using first sqldata.isEmpty()
in if then it will throw NullPointerException
.
Upvotes: 1
Reputation: 3509
you should use:
if (sqldata == null || sqldata.isEmpty()) {
sqldata = fileloader.dbhealthload();
}
At first you should check whether sqldata is null or not . After that you should check the emptyness.
Upvotes: 1
Reputation: 12880
You should not check for null this way,
// Invokes method on null when sqldata is null. so, this shouldn't be done
if (sqldata.isEmpty()||sqldata.equals(null)) {
sqldata = fileloader.dbhealthload();
}
Instead you need to check for null like this
if(sqldata == null || sqldata.isEmpty()){
sqldata = fileloader.dbhealthload();
}
Upvotes: 1
Reputation: 1975
Null references have no methods. Thus, you can't test null with equals
- instead, you should test it with the ==
operator.
Change your code to
if(sqldata == null || sqldata.isEmpty())
Upvotes: 1