简体   繁体   中英

How to handle null check in generic way in Java?

I have to check null condition for too many methods. how can i do it in generic way rather than repeating the same steps or Is it possible to write?

Here's my code:

<Student>
 <Name></Name>
 <RollNumber></RollNumber>
</Student>

I should throw an exception If user gives null value for Name or RollNumber field. I have already written code below. which is working correctly but i need to write generic method for both scenario and it should work the same as below,

if(student.getName() == null) {
 System.out.println("Name is required to create Student Data");
}

if(student.getRollNumber() == null) {
 System.out.println("RollNumber is required to create Student Data");
}

Note : Name is String and RollNumber is an Integer

Can you please give me an IDEA?

This question may already have an answer here: Avoiding “!= null” statements in Java? 44 answers

My Question is "I don't want avoiding null statement. If user gives null value How Can we throw an exception in generic way?"

If your Student bean always requires non-null values how about constructing the student object with non-null values inside a constructor. As you are doing the validation inside the constructor only no need to the do the same whenever you are using that object

public class Student
{
private String name;
private String rollNumber;

    public Student(String name, String rollNumber)
    {
        setName(name);
        setRollNumber(rollNumber);
    }   

    public String getName()
    {
        return name;
    }

    public String getRollNumber()
    {
        return rollNumber;
    }

    public String setName(String name)
    {
        if(isNull(name))
       {
         throw new NullPointerException("Name is required to create Student Data");
       }
       this.name= name;
    }

    public String setRollNumber(String rollNumber)
    {
        if(isNull(rollNumber))
       {
         throw new NullPointerException("Roll Number is required to create Student Data");
       }
       this.rollNumber = rollNumber 
    }

    private void isNull(String str)
    {
       if(str == null || str.trim().isEmpty())
          return true;
       return false;
    }

}

You should probably take a look at Hibernate Validation . Then you can simply annotate the fields of your bean with @NotNull .

As a bonus you can handle other validations as well.

You can use the Apache Commons String Utils API , which defines operations on String that are null safe.

You can do something like this:-

if (StringUtils.isEmpty(student.getName()) { //this check for both empty String and null and prevents null pointer exceptions

     System.out.println("Name is required to create Student Data");

}

Besides null comparison for just Strings, Assertions are good way to handle nulls you can check here for more info. Make sure to pass -ea argument to JVM as by default it ignores Assertions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM