choixPortCom.equals(null)
will never be true. If choixPortCom
is not null
, then the expression will return false as expected. If choixPortCom
is null
, then the expression will throw a NullPointerException
, since you are attempting to call a method on null
; this is what's happening in your case. The appropriate way to check for null
is:
if (choixPortCom == null) // I've assumed a more common naming convention
There is also an Objects
class in Java 7 that has some useful methods for null
-checking. For example, Objects.requireNonNull()
:
Objects.requireNonNull(choixPortCom, "input can't be null!")