-5

I just want to know why the compiler allow to continue the program normally in the first condition:

public void ButtonOnClickDirectoryList(View v){
    try {
        if (!spnOptionSelectedDepartment.equals(null) && !spnOptionSelectedCities.equals(null)) {
            if (!spnOptionSelectedTypes.equals(null)) { //code....}

the spnOptionSelectedDepartment and spnOptionSelectedTypes are Strings and are defined at the begining of the class like this:

private String spnOptionSelectedDepartment = null;
private String spnOptionSelectedCities = null;
private String spnOptionSelectedTypes = null;

so when I press the button, it call this method and this are the values that I have in the moment:

spnOptionSelectedDepartment = "9999"
spnOptionSelectedCities = null
spnOptionSelectedTypes = null

so when I put a break point on this condition it just continue validating the rest of the code inside that if... Could anybody explain me why this behavior?

Let me edit the question, Yes it throws nullpointer exception on the second if...

if (!spnOptionSelectedTypes.equals(null)) {

but why it allows the first IF when spnOptionSelectedCities = null...?

jugger
  • 1
  • 6
  • 3
    `if (spnOptionSelectedTypes != null)` – Blackbelt Feb 24 '15 at 16:06
  • 1
    The code posted above will throw a `NullPointerException` if `spnOptionSelectedDepartment` is `null`. – Luiggi Mendoza Feb 24 '15 at 16:07
  • You should use == to compare a string with null. equals is for comparing values of two strings. – yu_sha Feb 24 '15 at 16:08
  • @yu_sha you are the only one who give a real answer, the rest just mark "not use full" like they think everybody have to know everything... – jugger Feb 24 '15 at 16:17
  • @yu_sha I read a lot of how to compare values between strings and it says that the better way to know if a string have known value you sould use String.equal() ..... – jugger Feb 24 '15 at 16:20
  • @jugger Who told you that? I have some stones waiting to be thrown... – Gorcyn Feb 24 '15 at 16:28
  • For specific values, you can `"specific".equals(myStringVariable);` but that never works the other way. – Gorcyn Feb 24 '15 at 16:30
  • hmmmmm I get it now, well, When I was studying a Android Course the professor of the course use a lot that and told use that.... – jugger Feb 24 '15 at 16:34

1 Answers1

0

It shouldn't continue, it should throw a NullPointerException that you may intercept in the catch block.

When you try

if (!spnOptionSelectedTypes.equals(null))

it should throw this exception because spnOptionSelectedTypes is null so it is not a String and does not have any equals() method.

Edit:

It allows the first if to pass because it has 2 tests

if (A OR B) {

If A is true, the B condition is not tested because only one is required to continue, because of the OR operator.

Edit 2:

With:

if (A AND B) {

If A is true, B will also be tested and throw a NullPointerException if spnOptionSelectedCities is null.

Definitive answer:

Null tests in Java

if (x != null && y != null) {
    x.doSomething();
    y.doSomethingElse();
}
Gorcyn
  • 2,807
  • 1
  • 20
  • 22