Avoid NullPointerExceltions by using a getter for wrapped values with @Nullable annotation #835
Replies: 3 comments 2 replies
-
Using getters, and setters, would be good. I added a setter to |
Beta Was this translation helpful? Give feedback.
-
Not so sure anymore about that. When applying it to some new classes it did not felt like an improvement as the model classes get very boilerplate. Lombok helps to keep them minimal... Will need more time to get a clear opinion. |
Beta Was this translation helpful? Give feedback.
-
I guess we can close that. The extra pain with boilerplate does not justify the benefits IMO. |
Beta Was this translation helpful? Give feedback.
-
We should use always the
@Nullable
annotation if a value can be null.When using JavaFX properties or other swappers (Observeable) we cannot apply the
@Nullable
annotation as the wrapper is final and never null though the wrapped value can be null.To avoid running into NullPointerExceltions we should use a getter method when accessing the value and annotate the getter method with
@Nullable
.In the UI code we use direct field access as the model is static private and accessible. We should use getter methods instead to gain more control.
In case we have a nullable value we should postfix the property field with
Property
to make it more clearly distinguished from the getter.Beta Was this translation helpful? Give feedback.
All reactions