Java: avoid checking for null in nested classes (Deep Null checking)

前端 未结 11 492
失恋的感觉
失恋的感觉 2020-12-05 09:12

Imagine I have a class Family. It contains a List of Person. Each (class) Person contains a (class) Address. Each (class) Address contains a (class) PostalCode. Any "i

11条回答
  •  半阙折子戏
    2020-12-05 10:04

    and my favorite, the simple try/catch, to avoid nested null checks...

    try {
        if(order.getFulfillmentGroups().get(0).getAddress().getPostalCode() != null) {
            // your code
        } 
    } catch(NullPointerException|IndexOutOfBoundsException e) {}
    

提交回复
热议问题