0
if(status.equals(null))
{
  status="Pass";
}

from above code it throws NullPointerException, please give solution for comparing value with null.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
praveenraj4ever
  • 384
  • 1
  • 5
  • 14

7 Answers7

6

Looks like status itself is null. So when you do:

status.equals("null")

You're actually doing:

null.equals("null")

Which causes NPE. You should do:

if(status == null) //Now you're checking if the reference is null
                   //So you'll never dereference a null pointer

You might find this link useful.


Related topics:

What is null in Java?
Java null check why use == instead of .equals()

Community
  • 1
  • 1
Maroun
  • 94,125
  • 30
  • 188
  • 241
2

In Java "null" is a string. You want to compare the reference to null. Do the following:

if(status == null){
  // code here
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
Josnidhin
  • 12,469
  • 9
  • 42
  • 61
2

Keep in mind that String is class in Java.So whenever you call a method with unitialized object (which is a null object) then it will throw NullPointerException.In your case, there is possibility that your String status is not initialized So you need to make it sure.
BTW you should check null without using equals() method because it doesn't make sense that you are checking a null object's method for its null value.
You must do like this

if(status == null)
//Do something


only use equals method when you want to compare a String and at the stage where you are quiet sure that your String is initialized.Let say String status = ""; is a intialized String and now it is not null.
Just for the info
while using equals() , try to use it like "anyValue".equals(status) instead of status.equals("anyValue") because by using like "anyValue".equals(status), it will be more safe in case of null string and you wont get NullPointerException

Freak
  • 6,786
  • 5
  • 36
  • 54
0
if(status.equals("null")) //Just checking if string content/value is same
{
status="Pass";
}

By this you are just checking if value(content) of status variable is "null" String or not. IF you want to do a null check you need to do the following

if(null == status)
{
status="Pass";
}
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0

You are not suppose to make it null as "null"! in your case compiler consider it as string.

if(stringVariable!=null)
{
    //ur Code
}

(OR)

if(stringVariable.equals(null))
{
    //Ur code
}

In case if ur working with string array you can do it as following

if(stringArray.isEmpty())//checks length of String array 
{
//ur code
}
Mohan Raj
  • 590
  • 3
  • 8
  • 22
0

You cannot use .equals with a null . Use :

if(status == null) {
//Do something 
}

This is exactly the reason why the HashMap cannot store more than one null values . Because it always compares the key with other valuues and if a null is inputted the second time , it throws NPE .

Shuhail Kadavath
  • 448
  • 3
  • 13
0

You are getting this error since status is null.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115