-1

I trying to get info in database and sending for an object called Question.

When i got null in database and send it to the field of the object.. I suppose that field will be "null", cause the field is an String.

But i tried all ways to verify it and i cant...

I tried:

    Question a = new Question();
    a.setAnswer(database.getAnswer(id));
    // Way 1
    if(a.getAnswer() == null) {
            Log.d("Way 1", "successfully");
    } 
    // Way 2
    if(a.getAnswer() == "null") {
            Log.d("Way 2", "successfully");
    }
    //Way 3
    if(a.getAnswer().equals(null)) {
            Log.d("Way 3", "successfully");
    }
    // Way 4
    if(a.getAnswer().equals("")) { 
            Log.d("Way 4", "successfully");
    }

But I didn't got result...

I put in log to log my Question.getAnswer() too:

And here's the result:

    D/Here == >: null
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
DougBaltazar
  • 85
  • 4
  • 12

2 Answers2

0

In Way 2

use : if(a.getAnswer().equals("null")( {

This is the way two strings must be compared.

codingenious
  • 8,385
  • 12
  • 60
  • 90
0

Try this code for comparison

if(a.getAnswer().equals("")) {
    Log.d("Way 1", "successfully");
} 

This will work as expected

Naveed Yousaf
  • 133
  • 2
  • 13