Throwing exceptions on setting @NotNull fields to <null>

Answered

Hello!

If I use @NotNull (the JetBrains one) on return from methods and return <null> I get an IllegalStateException - great!
if I use @NotNull on method parameters and pass in a <null> I get an IllegalArgumentException - great!
But if I annotate local variables or instance variables with @NotNull and assign those <null> I will not get an exception. Is that possible to archive so they have the same behaviour as returns and parameters?
I have set "Add runtime assertions for notnull-annotated methods and parameters" in preferences. Ofcause that setting actually do not mention fields so I do not know why I would have any hope for fields to work too.

Workaround for instance variables are offcause to create getters/setters with @NotNull parameter check. But there is no way to do this for local variables, right?

Best regards,
- Morten Green Hermansen

0
1 comment

Morten G Hermansen No, you can't make it directly for fields, and you can't for variables. For fields you can indeed use annotated getters and setters. As for the local fields, IDEA analyse them quite well, so it won't make much difference if they annotated with '@NotNull'/'@Nullable' or not.

0

Please sign in to leave a comment.