Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validate long variable if null

Tags:

java

I am having difficultie while validating a long variable if it is null. The Code which I am using is :

long late_in= latein_hours.getTime();

It will show an error that java null pointer exception. So how can I validate if it is null then make it equal to zero.

Thanks

like image 956
maas Avatar asked Nov 27 '22 14:11

maas


2 Answers

long late_in = 0;
if(latein_hours!=null){
    late_in= latein_hours.getTime();
}

Primitive can't be null, only reference to object can hold null value

like image 159
jmj Avatar answered Dec 15 '22 23:12

jmj


A long can’t be null: if you don't set a value, is automatically set to 0.

If you want a variable that can be null (for example if not initialized), you must use Long (look the case). Long is like a container for long that can be null.

Long latein_hours;
long late_in;
if(latein_hours!=null){
    late_in= latein_hours.getTime();
}
like image 24
T30 Avatar answered Dec 15 '22 23:12

T30