You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I work on an Android App. I am trying to improve annotations used in our code to reduce NPEs. The code I am working has many instances where a subclass overrides a function in superclass and this function uses a @nullable or @MonotonicNonNull member variable of the subclass. Here is a make-up example
public abstract class AbstractViewFragment{
public void onLoad(){
// do something here
}
}
public class SpecificViewFragment implements AbstractViewFragments{
@Nullable private Object cursor;
@MonotonicNonNull protected Object viewDetail;
protected Object webView;
SpecificViewFragment(){
webView = new Object();
}
…
@override
public void onLoad(){
if (viewDetail.isType1()){
// do A
}else{
//do B
}
}
}
Nullness checker throws an error for using viewDetail.isType1() method as possible and in most cases, such errors are suppressed using checkNotNull(“viewDetail”) at the beginning of the SpecificViewFragment.onLoad() method and has caused NPEs as the callee of this function didn’t make sure that viewDetail is non_null.
If we try to add RequiresNonNull(“viewDetails”) to SpecificViewFragment.onLoad() it throws the error below:
error: [contracts.precondition.override.invalid] Subclass precondition is stronger for `this.var` in `overriddenFuntion`
As I understand, based on behavioural subtyping, precondition cannot be strengthened in the subtype.
But using checkNotNull is not preventing NPEs in the code either and is neither a great way to tell developers that they should be thinking about viewDetails being null. What would be a better solution in such cases?
The text was updated successfully, but these errors were encountered:
I work on an Android App. I am trying to improve annotations used in our code to reduce NPEs. The code I am working has many instances where a subclass overrides a function in superclass and this function uses a @nullable or @MonotonicNonNull member variable of the subclass. Here is a make-up example
Nullness checker throws an error for using
viewDetail.isType1()
method as possible and in most cases, such errors are suppressed usingcheckNotNull(“viewDetail”)
at the beginning of theSpecificViewFragment.onLoad()
method and has caused NPEs as the callee of this function didn’t make sure thatviewDetail
is non_null.If we try to add RequiresNonNull(“viewDetails”) to
SpecificViewFragment.onLoad()
it throws the error below:As I understand, based on behavioural subtyping, precondition cannot be strengthened in the subtype.
But using
checkNotNull
is not preventing NPEs in the code either and is neither a great way to tell developers that they should be thinking aboutviewDetails
beingnull
. What would be a better solution in such cases?The text was updated successfully, but these errors were encountered: