aspose file tools*
The moose likes Beginning Java and the fly likes how to check whether long value is null or not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to check whether long value is null or not" Watch "how to check whether long value is null or not" New topic
Author

how to check whether long value is null or not

Prabhudatta Choudhury
Greenhorn

Joined: Jul 03, 2009
Posts: 20

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.


Thanks
Prabhudatta
Ashwini Kashyap
Ranch Hand

Joined: Aug 30, 2012
Posts: 90
Try:

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

Thanks,
Ashwini Kashyap | www.infocepts.com
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
This does not even compile:

Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
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

Joined: May 22, 2012
Posts: 867
    
    5
rs is a ResultSet I presume.

ResultSet has this method:

Ashwini Kashyap
Ranch Hand

Joined: Aug 30, 2012
Posts: 90
Sorry my bad. It was : "".equals(..);
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
> 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

Joined: Sep 19, 2011
Posts: 20

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

Joined: Sep 19, 2011
Posts: 20

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

Joined: Jul 03, 2009
Posts: 20

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to check whether long value is null or not