• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to check whether long value is null or not

 
Prabhudatta Choudhury
Greenhorn
Posts: 20
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am getting some value from DB to do some kind of padding operation on it. But before that padding I need to check whether that value is null or not.

<CODE>
someObject.setValue(rs.getInt(6)); // where this value in DB is of type Number (31,5)
private long dBValue = someObject.getValue();

if(dBValue == null || dBValue.equals(null))
{
System.out.println("Padding space on it");
dBValue = (String)dBValue+"";

}

else{
// some more logic
}

</CODE>

When I am trying to check the value as null its giving compile time error. If I am converting the Long value to String to check the value its throwing NumberFormatException and NullPointerException in differenct scenarios.

Please help me in this.
I am looking for a solution where I will be able to check that long value as null.
 
Ashwini Kashyap
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:

if(null == dBValue || "".equals(dBValue)) {...}

Thanks,
Ashwini Kashyap | www.infocepts.com
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This does not even compile:

 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can not declare a local variable (that is, defined inside a function ) private.
Whereas a Long object reference can be null, a primitive long can not.

 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rs is a ResultSet I presume.

ResultSet has this method:

 
Ashwini Kashyap
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry my bad. It was : "".equals(..);
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> When I am trying to check the value as null its giving compile time error.

The compile errors come with line numbers and valuable hints.
A rule of thumb is that you should correct the first errors first, as the following ones can occur due to the previous ones.
 
Jinu Vijay
Greenhorn
Posts: 20
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interestingly dbValue is a primitive type and could never be null in the first place. If it was of type Long, then this should suffice to check for null values:

if(dBValue != null ) {
// Do something

} else {
// Handle null

}



 
Jinu Vijay
Greenhorn
Posts: 20
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to that ... your db column is a floating type and would have decimal (of precision 5 here). you are actually ignoring the decimal by doing a getInt.

You might want to do a getFloat / getDouble / getBigDecimal etc!
 
Prabhudatta Choudhury
Greenhorn
Posts: 20
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all.

I made that dBValue as BigDecimal and do the null-check operation on it.

Thanks again for help in short time.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwini Kashyap wrote:Sorry my bad. It was : "".equals(..);


That makes no sense. It has nothing to do with checking for null and nothing to do with long or Long.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic