连接安全导航运算符

class User {
  String name
  int age
}

def users = [
  new User(name: "Bob", age: 20),
  new User(name: "Tom", age: 50),
  new User(name: "Bill", age: 45)
]

def null_value = users.find { it.age > 100 }    // no over-100 found. Null 

null_value?.name?.length()    // no NPE thrown
//  null ?. name  ?. length()
// (null ?. name) ?. length()
// (    null    ) ?. length()
// null

null_value?.name.length()    // NPE thrown
//  null ?. name  . length()
// (null ?. name) . length()
// (    null    ) . length()  ===> NullPointerException

null_value?.name 上的安全导航将返回 null 值。因此,length() 必须对 null 值进行检查以避免 NullPointerException