I\'m new to android/java programming and am confused how to properly deal with this warning.
Method invocation \'\' may produce \'Java.lang.NullPointe
I've used Objects.requireNonNull()
which is a good way IMO. As @matiash mentioned, this is not a fool-proof way for every use case, but where you are sure that data won't be null
, you can use this approach to get rid of the warning. And if it does fail for some unknown reason, you will get NullPointerException
which you will get anyway without using this.
// before
cal.setTime(date);
// after
cal.setTime(Objects.requireNonNull(date));