Skip to content
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

[java] Deprecate AvoidThrowingNullPointerException #3296

Open
oowekyala opened this issue May 24, 2021 · 0 comments
Open

[java] Deprecate AvoidThrowingNullPointerException #3296

oowekyala opened this issue May 24, 2021 · 0 comments
Labels
is:deprecation The main focus is deprecating public APIs or rules, eg to make them internal, or removing them

Comments

@oowekyala
Copy link
Member

Is your feature request related to a problem? Please describe.
The documentation of AvoidThrowingNullPointerException just contradicts itself...

Avoid throwing NullPointerExceptions manually. These are confusing because most people will assume that the virtual machine threw it. To avoid a method being called with a null parameter, you may consider using an IllegalArgumentException instead

But then 2 lines later the Effective Java quote contradicts this:

If a caller passes null in some parameter for which null values are prohibited, convention dictates that NullPointerException be thrown rather than IllegalArgumentException.

We should indeed throw NullPointerException to check a parameter; this allows the exception to be the same whether your parameter validation was done explicitly with eg Objects.requireNonNull, or "implicitly" by eg calling a method on the parameter. I think this trumps the justification that "people will assume that the virtual machine threw it", which is a really weak argument... When you debug an NPE, the first thing you do is jump to the line where it was thrown. If the line is throw new NullPointerException(), it's pretty clear that the JVM didn't throw it...

Describe the solution you'd like Deprecate and remove AvoidThrowingNullPointerException

Describe alternatives you've considered

Additional context

@oowekyala oowekyala added the is:deprecation The main focus is deprecating public APIs or rules, eg to make them internal, or removing them label May 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is:deprecation The main focus is deprecating public APIs or rules, eg to make them internal, or removing them
Projects
None yet
Development

No branches or pull requests

1 participant