简体   繁体   English

如何在Java中以通用方式处理null检查?

[英]How to handle null check in generic way in Java?

I have to check null condition for too many methods. 我必须检查null条件是否有太多方法。 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. 如果用户为Name或RollNumber字段提供空值,则应该抛出异常。 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 注意:名称是字符串,而RollNumber是整数

Can you please give me an IDEA? 你能给我一个IDEA吗?

This question may already have an answer here: Avoiding “!= null” statements in Java? 这个问题可能已经在这里有了答案:避免在Java中使用“!= null”语句? 44 answers 44个答案

My Question is "I don't want avoiding null statement. If user gives null value How Can we throw an exception in generic way?" 我的问题是“我不想避免使用null语句。如果用户提供null值,我们如何以通用方式引发异常?”

If your Student bean always requires non-null values how about constructing the student object with non-null values inside a constructor. 如果您的Student bean总是要求非空值,那么如何在构造函数中使用非空值构造Student对象。 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 . 您可能应该看看Hibernate Validation Then you can simply annotate the fields of your bean with @NotNull . 然后,您可以简单地使用@NotNull注释bean的字段。

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. 您可以使用Apache Commons String Utils API,该API定义对String的安全为空的操作。

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. 除了仅对字符串进行空值比较外, Assertions是处理nulls好方法,您可以在此处查看更多信息。 Make sure to pass -ea argument to JVM as by default it ignores Assertions 确保将-ea参数传递给JVM,因为默认情况下它会忽略Assertions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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