[RESOLVED] Compilation error when adding null object to list

Hello MicroEJ,

When I try to add null to my List, I get the following compiler error:
Null type mismatch: required '@NonNull Object' but the provided value is null

I don’t understand because I don’t get this error when developing a Java application.

Best regards,
Alain

Hi @alain

By default, the Java Collections Framework allows implementations of java.util.List to support the null entry.

However, using null entries in collections is quite rare in practice, so keeping this behavior when Null Analysis is enabled would produce lots of unwanted errors and would force to explicitly write lots of unwanted assertions in most cases.

Therefore, we decided to disallow the use of null entries in collections for the benefit of Null Analysis. That’s why java.util.List.add(Object) requires a non null object when Null Analysis is enabled (the java.util package is marked with the NonNullByDefault annotation).

However, the implementation is left unchanged, so you can still use null entries by relaxing the Null Analysis rules of the project using it.

–Frédéric

Ok thank you @frederic.riviere, i think i will refactor my class to avoid relying on null values.

Best regards,
Alain