0

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?"

Azhaguvel A
  • 631
  • 2
  • 10
  • 24
  • 3
    Take a look at Guava's [`Optional` class](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Optional.html) – toniedzwiedz Jul 18 '14 at 04:42
  • 3
    Java 8 has Optionals feature take a look at it – Dungeon Hunter Jul 18 '14 at 04:45
  • possible duplicate of [Avoiding "!= null" statements in Java?](http://stackoverflow.com/questions/271526/avoiding-null-statements-in-java) – azurefrog Jul 18 '14 at 04:45
  • If there is a way to get all these things into some type of collection or array, you could iterate over them and check if any is null. BTW, a lot of XML parsers will give you an empty string instead of null for an empty tag. Better check for that, too. – Kevin Krumwiede Jul 18 '14 at 04:46
  • I would write the code in this way while constructing the Object itself i wont allow null values in those fields – Dungeon Hunter Jul 18 '14 at 04:48

3 Answers3

2

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;
    }

}
Dungeon Hunter
  • 19,827
  • 13
  • 59
  • 82
0

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.

bowmore
  • 10,842
  • 1
  • 35
  • 43
0

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

Community
  • 1
  • 1
Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28