How to detect if a long type is actually NULL?

44,672

Solution 1

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.

Solution 2

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);
  // ...
}

Solution 3

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.

Share:
44,672
Mechlar
Author by

Mechlar

I am a senior software developer and team/project manager. I develop with Javascript, Angular, React, NodeJS, Firebase, jQuery, CSS, Bootstrap, HTML5, etc, etc.

Updated on June 19, 2020

Comments

  • Mechlar
    Mechlar almost 4 years

    We have a nullable (type long) column (named referral) in our MySQL database. We use hibernate for ORM.

    I am trying to get the value of the column for a given member. Some are null, and if its not, its an id that points to another member whose is the referrer.

    The problem is in the java code I am trying to detect if that member's column is null, if not, do something.

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

    member.getReferral() returns the value (type long) of the referral column. Some of those columns are null and some are not.

    The above code compiles fine, but I get a nullPointerException when I call the method on a user whose referral column is null.

    How do I properly do a detection on this?

    Thanks in advance!

    Full Answer:

    Thanks to @Marcelo for the best correct answer.

    Here is the code in its final state:

    Long referrerAffiliateId = member.getReferral();
    if (referrerAffiliateId != null) {
        //...
    }
    
    • Ted Hopp
      Ted Hopp over 12 years
      What's the return type of member.getReferral()?
    • gnomed
      gnomed over 12 years
      I assume you mean Long since a long is a primitive type and is not possible to have it be null. Long can be null though. Check the return value of getReferral() directly and do not attempt to convert it to a string.
    • rfeak
      rfeak over 12 years
      Based on the comments and your responses, I'd say it's time for an actual stack trace to see where they error really is occurring. I suspect there's something inside getReferral() that is auto-unboxing and causing an NPE. The Source for getReferral() may also be helpful.
  • Mechlar
    Mechlar over 12 years
    Thanks, but it doesn't return "Long", it returns "long", which as I understand it, is not nullable where "Long" can be.
  • anubhava
    anubhava over 12 years
    As per OP member.getReferral() returns long not Long.
  • Marcelo
    Marcelo over 12 years
    @Dale If you want to detect nullability for the referral property using Hibernate, you must change the data type of the property to Long. Otherwise, member.getReferral() will return 0 when the column value is null.
  • Ted Hopp
    Ted Hopp over 12 years
    OP said that member.gerReferral() returns a primitive long, and specifically not a Long.
  • Marcelo
    Marcelo over 12 years
    You will not get an exception anymore, but you will never detect nullability of the column.
  • Mechlar
    Mechlar over 12 years
    I chose this solution because the answer uses less code, is cleaner, and cause @Marcelo made the suggestion about the data type change in hibernate.