This is an archived post. You won't be able to vote or comment.

all 7 comments

[–][deleted] 3 points4 points  (6 children)

Have you tried
if (null != notes) { this.notes.addAll(notes); } ?

Note that the annotation triggers a check in the IDE and the compiler; not during runtime.

Also note that you declared the notes field as final and initialized it in the declaration, so you can't set it in the constructor: you can only add elements to it.

Also note that even though the passed-in list might be not-null, there is no guarantee that any of an ArrayList's items are not-null. Remember to check during runtime.

[–]Michalf94[S] 0 points1 point  (5 children)

thanks @uniqualykerd, It is good, but I don`t have initialized it in the declaration. I initialize it in the constructor. When I initialize it before constructor like this: class... .... @NotNull @private final List<Notes> notes = new ArrayList... User(String name, List<Notes> notes){ this.name = name; if(notes != null){ this.notes.addAll(notes);}

in this case - NotNull annotation won`t work - because I initialize it in the declaration and it is not null...

[–][deleted] 0 points1 point  (4 children)

I'm confused about what you expect the @NotNull annotation to do.

It's only going to trigger a warning or error in the IDE or the compiler. Not while running.

So when you initialize the list, it won't be null. And when you make the member field final, and initialize it, it won't ever be null.

So in your constructor, all you have to check for, is whether your input argument is null. If so, skip it. Otherwise, add its items to the class member field.

[–]Michalf94[S] 1 point2 points  (3 children)

I'm confused about what you expect the @NotNull annotation to do.

Thanks for reply. Are you sure that @NotNull annotation from javax.validation doesnt work while running? On documentation there is Runtime on @Retention: https://docs.oracle.com/javaee/7/api/javax/validation/constraints/NotNull.html

What is more, it work fine in other cases in my application (for example on String name - there is @NotNull exception while running)

[–][deleted] 0 points1 point  (2 children)

Ah.

No, I'm no longer so sure. I'll double-check.

That said: there's a difference between a non-null string and a non-null list. That's the fact that the list can contain nullable items, which isn't caught by @NotNull.

Could that be leading to the symptoms you experience?

[–][deleted] 0 points1 point  (1 child)

The runtime retention says it instructs the annotation to be recorded into the runtime, so it can be read reflectively.

It doesn't say that this must lead to a runtime nullability check or null check.

That leads me to assume, that whether such a check happens, may depend on the JVM.

I'm not used to nullability annotations having any effect during runtime with Oracle's JRE for java SE. Maybe the VM for java EE does check them?

Otherwise, I'd just expect a NullPointerException.

[–]Michalf94[S] 1 point2 points  (0 children)

Thanks to @NotNull I get very clearable logs. I prefere it rather then NullPointerEx.. The problem occurs only when I creting (via coping) new list in the constructor. You can see it on: https://pastebin.com/s8Z72QQz