-4

I'm new in java programming. Now I have a problem when I have String variable equals to null. When I check condition on that String it error :

Exception in thread "main" java.lang.NullPointerException.

This is my code :

public class Test {
    static String a=null;
    public static void main(String[] args) {
        if(a.equals(null)){
            System.out.println("Null");
        }
    }
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Holi Boom
  • 1,346
  • 1
  • 12
  • 29

4 Answers4

4

Instead of a.equals(null) you should be doing a == null.

if(a == null) {
    System.out.println("Null");
}

Alternatively, you can use the StringUtils to check if String is null or empty by doing:

if(StringUtils.isEmpty(a)) {
    System.out.println("Null");
}
user2004685
  • 9,548
  • 5
  • 37
  • 54
  • Why i cannot use equals() method? – Holi Boom Mar 07 '16 at 07:11
  • 4
    Because if `a` itself is `null`, it will be `null.equals()` and it does not hold good. You can't invoke a method on a `null` object. – user2004685 Mar 07 '16 at 07:12
  • 1
    @user2004685: Well, on a `null` *reference*. There's no such thing as a `null` object. It's worth keeping the distinction between variables, references and objects very clear. – Jon Skeet Mar 07 '16 at 07:15
  • @user2004685 i cannot use StringUtils.isEmpty() method. why? – Holi Boom Mar 07 '16 at 07:21
  • 1
    @Huo Chhunleng, you need to add org.apache.commons.lang3 dependency to your project. If you are using maven, then choose it [here](http://mvnrepository.com/artifact/org.apache.commons/commons-lang3) – WeGa Mar 07 '16 at 08:21
  • @JonSkeet Yes, you are right. It should have been `null` reference and not object. @WeGa Thanks for pointing that out. – user2004685 Mar 07 '16 at 09:29
2

Check with == or != like:

if(a!=null){
    System.out.println("is not Null");
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Why i cannot use equals() method? – Holi Boom Mar 07 '16 at 07:11
  • 1
    https://docs.oracle.com/cd/E19798-01/821-1841/gkcrg/index.html – ΦXocę 웃 Пepeúpa ツ Mar 07 '16 at 07:13
  • Better in practice , Instead of using If(a!= null) you should use if(null!=a) – ManthanB Mar 07 '16 at 07:13
  • 4
    @ManthanB: No, that's a terrible idea: a) it's no safer; b) it's less readable. These "Yoda conditions" may make sense in *some* languages where the type of the condition in an `if` statement doesn't have to be a Boolean type, but it makes no sense in Java for situations like this. – Jon Skeet Mar 07 '16 at 07:16
  • Thanks @JonSkeet for the comment. – ManthanB Mar 07 '16 at 07:20
  • It's not "better" to reverse the comparison, 'null != a' vs. 'a != null', because both expressions are exactly equivalent. Equality and inequality are symmetric. If anything, the reversed expression is worse, because everyone recognizes it as reversed, which slows down mental processing of the construct. There is no "practice" to the reversal; it just makes code look worse. – Lew Bloch Mar 07 '16 at 08:17
2

If there is a potential for both references to be null, you can use

Objects.equals(a, b)
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

If you want to check for null

if(a == null){
  System.out.println("I am null");
}

If you want to check for empty or blank String

if(a != null && a.length() == 0){
  System.out.println("I am an empty String");
}
HJK
  • 1,382
  • 2
  • 9
  • 19