Reputation: 12234
I saw in some groovy code this:
trip.id?.encodeAsHTML()
What is the difference between using or not using "id?."?
Upvotes: 3
Views: 343
Reputation: 346526
It's called the "null-safe dereferencing operator". The difference is that if trip.id
is null, instead of throwing a NullPointerException
, it will return null
as the result of the method call.
Upvotes: 4
Reputation: 4600
It's the Groovy null-safe operator. It performs a null check before dereferencing the object. See more on Groovy Operators here.
Upvotes: 1
Reputation: 76
It checks if the object is null or not. Using it, you can prevent nullpointer exception.
If you use it, you should use it for the whole object (eg: trip.id?.otherstuff?.morestuff?.encodeAsHTML()
Upvotes: 6