3

I use Firebase Database and have message object written to it with one boolean field. When I try to read that object with getValue(Boolean.class) I get an exception. It only happens when getting this boolean value, I get string with no problem.

Method causing exception:

@Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        message.setSenderId(dataSnapshot.child("senderId").getValue(String.class));
        message.setDestinationId(dataSnapshot.child("destinationId").getValue(String.class));
        message.setDatetime(dataSnapshot.child("datetime").getValue(Date.class));
        message.setText(dataSnapshot.child("text").getValue(String.class));
        message.setSent(dataSnapshot.child("isSent").getValue(Boolean.class)); // this line causes NullPointerException
}

My Message model class:

@IgnoreExtraProperties
public class Message {

    @Exclude
    private String id;
    @Exclude
    private ValueEventListener valueListener;
    @Exclude
    private Conversation destination;

    // User ID
    private String senderId;
    // Conversation ID
    private String destinationId;
    private Date datetime;
    private String text;
    private boolean isSent = false;

    public Message(String id, String sender, String destination, Date date, String text) {

        this.id = id;
        this.senderId = sender;
        this.destinationId = destination;
        this.datetime = date;
        this.text = text;
    }

    public Message() {

    }

    public Message(String id, Conversation destination) {

        this.id = id;
        this.destination = destination;
    }

// ...

public boolean isSent() {

        return isSent;
    }

    public void setSent(boolean sent) {

        isSent = sent;
    }

}

Example of message stored in database:

{
  "datetime" : {
    "date" : 12,
    "day" : 4,
    "hours" : 17,
    "minutes" : 32,
    "month" : 9,
    "seconds" : 25,
    "time" : 1507822345776,
    "timezoneOffset" : -120,
    "year" : 117
  },
  "destinationId" : "test_conversation",
  "isSent" : true,
  "senderId" : "test_sender",
  "text" : "hello world"
}

What is wrong with that code? I tried to figure it out but I still didn't come up with anything.

fpiechowski
  • 517
  • 1
  • 7
  • 21
  • Can you share an example of the exception from logcat? Is there ever a case within your database that the `isSent` value doesn't exist (is therefore `null`)? – Grimthorr Oct 12 '17 at 16:10
  • Please share your logs where you get the exception. – Sarfaraz Oct 12 '17 at 16:26
  • please screen shot your database console. It is possibe that field `isSent` is not exists – Faruk Oct 12 '17 at 17:01

4 Answers4

4

Try to change isSent variable's type to Boolean.

It will help, in case when dataSnapshot.child("isSent").getValue(Boolean.class) returns null, cause reason of exception in calling method setSent(boolean) with null.

Yegor Babarykin
  • 705
  • 3
  • 12
1

try replacing

public void setSent(boolean sent) {

    isSent = sent;
}

with

public void setSent(boolean sent) {

    this.isSent = sent;
}
Simo
  • 345
  • 4
  • 12
1

I'm using boolean and never been a problem. But i've experience same problem once, it's because in the database it saved with field sent not isSent. Can you screenShot your database from your console?

My solution is to put @PropertyName("isSent") before your getter and setter

@PropertyName("isSent")
public boolean isSent() {

    return isSent;
}

@PropertyName("isSent")
public void setSent(boolean sent) {

    isSent = sent;
}

I'm afraid the field in your database is written "sent" : true not "isSent" : true

Faruk
  • 5,438
  • 3
  • 30
  • 46
0

To me, it looks like there is a mismatch between boolean (the primitive) and Boolean (the class). Would you like to fix that and try again? Replace all primitive declarations in your Message Model class with Boolean (the class). Let me know how it goes.

Abhishek
  • 1,261
  • 1
  • 13
  • 30