Following scenario throws nullptr exception inside if condition block !sample?.SomeField__c:
MyObject__c sample = null;
if (!sample?.SomeField__c) {
...
}
Why is that? Shouldn't that be evaluated to !(sample == null ? null : sample.SomeField__c)
?
if (!null) {
you get "! operator can only be applied to boolean expressions" as that is not a valid expression in Apex. Perhaps the thinking is better to output an NPE error in this case. Something likeif (sample?.SomeField__c === false) {
might be the way to go. Or probably better to add an extra line of code to establish the boolean value before the if.null or false
?sample?.SomeField__c !== true
maybe.Error: Comparison arguments must be compatible types: NULL, Boolean.
I think that using simple inequality operator will be enough :)(Boolean) sample?.SomeField__c !== true
.