-3
 filterType = 0;

            if((Name !=  " " &&  !Name .isEmpty() ) |( userName != " " && !userName.isEmpty() ) | userLock == true | (HQ !=  " " && !HQ.isEmpty() ) |  Area !=  0.0  | Route != 0.0 | ( Label != " " && !Label.isEmpty() )){

                filterType = filterType + 1;
            }



            if( (Phone != " " && !Phone.isEmpty() ) | (URL != " " && !URL.isEmpty()) |( City != " " && !City.isEmpty() ) |  (Date != " " && !Date.isEmpty())| (Email != " " && !Email.isEmpty()) ){
                filterType = filterType + 2;

            } 

If any of above string contains value then filter type should be added but java throws null pointer exception. How can I fix it?

Code_Ninja
  • 1,729
  • 1
  • 14
  • 38
nikita
  • 11
  • 1
  • 2
    `name!=null` string not `null` – SatyaTNV Jul 27 '15 at 14:25
  • 2
    userName.isEmpty() will give you a nullpointer exception if it's null, so check that it's `!= null` first – BoDidely Jul 27 '15 at 14:26
  • 3
    Read http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java and then think carefully about what that will do in your case if `Name` is a null reference... – Jon Skeet Jul 27 '15 at 14:26
  • I can see that you are a new user here, so I'm gonna give you a tip: When Jon Skeet writes, read, and do what he says :). He is the highest ranked member of this community, and by that, one of the most important people of computer programming in the world. – jumps4fun Jul 27 '15 at 14:33
  • @Kjetil: There are too many things wrong with this code to find a good duplicate. nikita: Using | for "or" is a bad idea here, use || instead (since it short-circuits). it's totally unclear to me what the logic should be, possibly a misunderstanding of boolean logic is involved? You probably should read up on DeMorgan's rule. – Nathan Hughes Jul 27 '15 at 15:28

2 Answers2

0

When checking if a String is null in Java, you could use

name == null. This returns true, if your String is in fact null

Note that I have deliberately changed the variable name from Name to name. variable names should never start with an upper case letter, as that is conventionally used for class definitions. Pretty much every Java developer would find your code hard to read, if you do not follow this convention.

jumps4fun
  • 3,994
  • 10
  • 50
  • 96
0

Proceed like this:

if(name != null && !" ".equals(name) && !name.isEmpty()) {
    ....
}
  • Evaluate if an object is null before calling a method on it
  • Prefer " ".equals(name) instead of name.equals(" ") because is other situations (not this one) name may be null
  • Never compare a String value with == but always use equals()
    How do I compare strings in Java?
  • | is the bitwise or : maybe did you mean the short-circuit or || in your code? See the Nathan Hughes' comment
  • Consider to name always variables in lowercase and camel casing letters
  • Drive only if not drunk

... or consider to write a function to test common invalid values:

static boolean isUseless(String value) {
    return value == null || " ".equals(value) || value.isEmpty();
}

... or convert your strings before use:

static String convertToNotNull(String value) {
    return value == null ? "" : value.trim();
}
Community
  • 1
  • 1
fantaghirocco
  • 4,761
  • 6
  • 38
  • 48