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

5
  • 3
    Take a look at Guava's Optional class Commented Jul 18, 2014 at 4:42
  • 3
    Java 8 has Optionals feature take a look at it Commented Jul 18, 2014 at 4:45
  • possible duplicate of Avoiding "!= null" statements in Java? Commented Jul 18, 2014 at 4: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. Commented Jul 18, 2014 at 4:46
  • I would write the code in this way while constructing the Object itself i wont allow null values in those fields Commented Jul 18, 2014 at 4:48

3 Answers 3

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

}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.