×

Loading...

我公司的解决方案。

Consider this code:
var aList = getAListOfStrings()
if(aList.get(0).isEmpty()) {
print("The first string is empty")
}
This code can cause a NullPointerException if either the list or the first string in the list is null. We can address this by using the null-safe invocation operator ?.:

var aList = getAListOfStrings()
if(aList?.get(0)?.isEmpty()) {
print("The first string is empty")
}
The null safe invocation operator works on both methods and properties.
Report

Replies, comments and Discussions: