How to detect if a long type is actually NULL?

The exception probably comes from Long.toString(), try checking the value before converting to a string:

Long ref = member.getReferral();
if (ref == null) {
  // Do something...
} else {
  String referrerAffiliateId = Long.toString(ref);
  // ...
}

Change

String referrerAffiliateId = Long.toString(member.getReferral());
if (referrerAffiliateId != null){
    //do something
}

To:

if (member.getReferral() != null){

    String referrerAffiliateId = Long.toString(member.getReferral());
    //do something
}

It's likely that you're getting the NullPointerException when you call Long.toString() with a null parameter.


Assuming member.getReferral() returns a Long, use:

if (member.getReferral() != null)

In Hibernate, if you want to be able to detect nullability in a property, you must not use primitive types, because they will always have a default value 0 for longs.