10

I was writing code, when IntelliJ suggested me a correction on:

objectOne.equals(objectTwo);

telling me that the Method invocation equals may produce the good old java.lang.NullPointerException , proposing as solution something that I wasn't aware of, Objects.equals:

Objects.equals(objectOne, objectTwo);

Reading the documentation I see only one potential problem, which is that if objectOne == null and objectTwo == null , then the result is true.

At this point the question is: could I start replacing and using everywhere this method instead of .equals ? Is this a safe approach or I'm missing some big contraindication?

Getting rid of NPE is very attractive... Thanks

10
  • 14
    Why is null == null being true a problem?
    – dustytrash
    Sep 20, 2018 at 15:27
  • @dustytrash I think this can produce an unwanted behaviour which is hard to find: if I have 2 nulls I will fast notice with .equals cause of NPE , but with Objects.equals the code will pass through that as a true result... it smells like potential hidden bug
    – Leviand
    Sep 20, 2018 at 15:29
  • @dustytrash Principle of Least Astonishment. Many languages treat null as not equal to anything
    – Jeutnarg
    Sep 20, 2018 at 15:29
  • 5
    could I start replacing and using everywhere this method instead of .equals: no. A null variable should be the exception. If a variable can be null, then using Objects.equals() is an option. Otherwise,if a variable is not supposed to be null, and you get an NPE, that means the code has a bug, and the bug should be fixed.
    – JB Nizet
    Sep 20, 2018 at 15:30
  • 2
    The correct way to handle this situation depends entirely on the context of this comparison. How are objectOne and objectTwo being used after this line of code? Please edit your question to show a complete method to illustrate. Sep 20, 2018 at 15:35

3 Answers 3

8

There is NO bulletproof answer for this question, as it depends on the needs of the program and how it should answer to that exceptional scenario. Saying that, I would say:

If having a null is a programmer's error (I mean something that you will never expect to happen inside your algorithms) I would suggest to leave the .equals method as a null pointer exception will arise and make you take notice of the problem, I mean, a programmer's error (Those errors that we don't want!, that means that we wrote bad an algorithm), and you should thanks the application for reporting it (instead of an angry real client....)

But if in the other hand your application could work as well, not considering that as a programmer error, then using Object.equals will fit better to your needs.

The general wisdom suggest that exceptions should at least be logged.

Summary

Try to investigate about different kinds of exceptions. Not all the exceptions are programmer errors.

In Java, usually, the checked exceptions are for all those events that you can anticipate, you can write code that should be able to handle. And the unchecked Exceptions (those that belongs the RuntimeException's family) are for events that you just can not anticipate (like null pointer exceptions), thus it is impossible to write code to handle things that you don't expect (instead you should correct the already existing code, not writing new code!).

8
  • i try to say a bullet proff answer, thanks for pointing that out
    – Victor
    Sep 20, 2018 at 15:34
  • Did you mean bulletproof?
    – dustytrash
    Sep 20, 2018 at 15:35
  • right! please be my guess and edit my answer, i'm not (ENGLISH) native speaker. Thanks for the help
    – Victor
    Sep 20, 2018 at 15:36
  • @Victor Already fixed. Needed a little more context to understand what you meant. Sep 20, 2018 at 15:37
  • 1
    This is probably what I was looking for... It's better to not using that as default, but adapting that on the situation. Thanks
    – Leviand
    Sep 20, 2018 at 15:56
7

It is an appropriate approach if you want to allow null values. Otherwise you could just add a check for null values and handle them appropriately.

4

Depends on your code and what 'everywhere' means. If your code currently assumes (maybe not explicitly but just by the fact that null input will never propagate beyond the equals call ) that null input will result in an NPE, then switching to Objects.equals will introduce a bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.