-
-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Null pointer exception on contains on set #1186
Comments
Please see the Javadocs:
... which links to the javadoc of
As they disallow |
I do not understand this logic. As they do not allow null elements, any operation trying to insert a null should throw. Though, read operations should just read. But as you say, this is a matter for upstream. |
The point is I use the Set interface, so I have no Idea the set was created using Set.Of(). It can contain null if created by other Set implementations. I worked around it by using streams. (So it breaks the Set interface) |
(I'm also moving various usages away from java.util.Set.of() due to such behavior).
I guess not. The issue is not creating a Set with null. The issue is reading a Set (not holding nulls) with This is also referred in https://stackoverflow.com/questions/70838768/why-list-containsnull-throwing-null-pointer-exception The valid point is in https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Collection.html#optional-restrictions:
I have hard times understanding the benefits of throwing rather than returning false, especially in the era of lambda and the ability to call Though, it is a feature as per OpenJDK choices, and not a bug in Adoptium binaries. (Just to make it clear to future readers, including myself when I'll get hit again by this item). |
Please provide a brief summary of the bug
Did you test with the latest update version?
Please provide steps to reproduce where possible
No response
Expected Results
print false
Actual Results
Cannot invoke "Object.equals(Object)" because "o" is null
java.lang.NullPointerException: Cannot invoke "Object.equals(Object)" because "o" is null
at java.base/java.util.ImmutableCollections$Set12.contains(ImmutableCollections.java:817)
What Java Version are you using?
openjdk 23.0.1 2024-10-15 OpenJDK Runtime Environment Temurin-23.0.1+11 (build 23.0.1+11) OpenJDK 64-Bit Server VM Temurin-23.0.1+11 (build 23.0.1+11, mixed mode, sharing)
What is your operating system and platform?
Ubuntu 24.04 .1LTS
How did you install Java?
No response
Did it work before?
No response
Did you test with other Java versions?
No response
Relevant log output
No response
The text was updated successfully, but these errors were encountered: