简体   繁体   English

如何在if条件下避免NullPointerException

[英]How to avoid NullPointerException in if condition

I have to take few actions depending upon the if condition. 我必须根据if条件采取一些措施。 Say I have an enum "VoucherType" 说我有一个枚举“ VoucherType”

Now I have a code, that is executed depending upon the condition:- 现在我有一个代码,根据条件执行:

private boolean verifyGiveAwayAccounting(GiveAwayMoneyVerificationEvent event) {

    if(event.getVoucherType().equals(VoucherType.GIVE_AWAY_MONEY_ON_SIGNUP)){
        someAction();
    }
    return verifySystemAccountTransaction(event);
}

I have to perform someAction() if the event is of type "GIVE_AWAY_MONEY_ON_SIGNUP" . 如果事件的类型为“ GIVE_AWAY_MONEY_ON_SIGNUP”,我必须执行someAction()。 But I do not have to do anything extra is event type is anything other than "GIVE_AWAY_MONEY_ON_SIGNUP". 但是我不需要做任何其他事情,因为事件类型不是“ GIVE_AWAY_MONEY_ON_SIGNUP”。 So when I call this code I set the voucherType to "GIVE_AWAY_MONEY_ON_SIGNUP" and someAction() is executed. 因此,当我调用此代码时,我将voucherType设置为“ GIVE_AWAY_MONEY_ON_SIGNUP”并执行someAction()。

But for any other type of events, I get null pointer exceptions in the if condition as I never set the voucher type as I do not want to do anything special. 但是对于任何其他类型的事件,由于没有设置凭证类型,因为我不想做任何特殊的事情,所以在if条件下会得到空指针异常。 So in order to avoid the nullPointerException I set the Voucher code to something dummy(other voucherType values) which I never use in any conditions. 因此,为了避免nullPointerException,我将Voucher代码设置为虚拟对象(其他voucherType值),在任何情况下都不会使用。 I there a sophisticated way I can eliminate the nullPointerException without initializing the VoucherType in event? 我有一种复杂的方法可以消除nullPointerException而不在事件中初始化VoucherType吗?

you should always test if your object is not null before making any test on this object's attributes. 在对该对象的属性进行任何测试之前,应始终测试对象是否不为null。

if(enum != null && enum.Football) {
  //some action
}

If event is never null , in this case perhaps you can invert your test : 如果event从不为null ,那么在这种情况下,您可以反转测试:

private boolean verifyGiveAwayAccounting(GiveAwayMoneyVerificationEvent event) {
    if(VoucherType.GIVE_AWAY_MONEY_ON_SIGNUP.equals(event.getVoucherType())){
            someAction();
    }
    return verifySystemAccountTransaction(event);
}

otherwise you should test if event is not null before : 否则,您应该在以下event之前测试event是否为null

private boolean verifyGiveAwayAccounting(GiveAwayMoneyVerificationEvent event) {
    if(event != null && VoucherType.GIVE_AWAY_MONEY_ON_SIGNUP.equals(event.getVoucherType())){
            someAction();
    }
    return verifySystemAccountTransaction(event);
}

As defined by WikiBooks: 根据WikiBooks的定义:

A NullPointerException is thrown when an application attempts to use an object reference, having the null value. 当应用程序尝试使用具有空值的对象引用时,将引发NullPointerException。 These include: Calling an instance method on the object referred by a null reference. 其中包括:在由空引用引用的对象上调用实例方法。

If you do not instantiate the enum value, it will have a value of null . 如果不实例化枚举值,则它将具有null Thus, the program is attempting to reference an object that contains null , which throws a NullPointerException . 因此,程序试图引用包含null的对象,该对象将抛出NullPointerException

Therefore, no, there is no way to avoid your NullPointerException . 因此,没有,没有办法避免NullPointerException You need to instantiate variables before attempting to reference them. 您需要实例化变量,然后再尝试引用它们。

I would check if the enum is not null before attempting to check the value of enum.Football. 在尝试检查enum.Football的值之前,我将检查enum是否不为null。

    void method(){
        if(enum!=null && enum.Football){
            SomeAction();
        }
    }

Apart from the already mentioned answers of checking for null, another possibility would be to actually create an extra enum value that represents null (a "dummy value") and use that as the default value: 除了已经提到的检查空值的答案之外,另一种可能性是实际创建一个表示空值的额外枚举值(“虚拟值”)并将其用作默认值:

public enum VoucherType {
    UNDEFINED, 
    GIVE_AWAY_MONEY_ON_SIGNUP,
    //....
    ;
}

Define "UNDEFINED" as default value: 将“未定义”定义为默认值:

public class Event {
    private VoucherType voucherType = VoucherType.UNDEFINED;

    public Event() {
    }

    public VoucherType getVoucherType() {
        return this.voucherType;
    }

    public void setVoucherType(VoucherType voucherType) {
        if(voucherType==null) {
            throw new IllegalArgumentException(); // make sure that voucher type cannot be set to null
        }
        this.voucherType=voucherType;
    }
}

That way events will never have null as voucherType and instead the enum value UNDEFINED. 这样,事件将永远不会具有null作为voucherType,而是枚举值UNDEFINED。

Warning: A lot of people would prefer recieving a NullPointerException instead of the above solution to immediatly get feedback when they forget to set the voucherType. 警告:很多人宁愿收到NullPointerException而不是上述解决方案,以免忘记设置voucherType而立即获得反馈。 With the above solution making the error of forgetting to set the voucherType and not realizing it (Because code throws no error) is much easier. 使用上述解决方案,使忘记设置voucherType而不实现它的错误变得更加容易(因为代码不会抛出错误)。

Also it might force you to check if the voucherType is still UNDEFINED for some operations where the requirement is that it is set to a meaningfull value. 另外,如果某些操作要求将voucherType设置为有意义的值,则它可能会强制您检查voucherType是否仍未定义。

I would actually rather check for null myself, but since you said you want other solutions I thought I post this anyway. 我实际上宁愿自己检查是否为null,但由于您说过您想要其他解决方案,所以我认为我还是张贴了此内容。

Simply invert the operands of the equals: 只需反转等于的操作数:

if(VoucherType.GIVE_AWAY_MONEY_ON_SIGNUP.equals(event.getVoucherType()))

It will never give you NullPointerException for a null value return of your getVoucherType() method. 对于getVoucherType()方法的空值返回,它永远不会给您NullPointerException。 Of course you must assure that event Object is never null. 当然,您必须确保事件Object永远不会为null。

Or, as suggested in one of the comments use the == operator, it's possible for enum: 或者,正如其中一项注释中所建议的,使用==运算符,可能会产生枚举:

if(VoucherType.GIVE_AWAY_MONEY_ON_SIGNUP == event.getVoucherType())

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

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