我使用Firebase数据库并使用一个布尔字段为其写入消息对象。当我尝试用getValue(Boolean.class)
读取该对象时,我收到一个异常。它只发生在获得这个布尔值时,我得到没有问题的字符串。Firebase数据库使用getValue时的NullPointerException(Boolean.class)
方法引起的异常:
@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
}
我的消息模型类:
存储在数据库中的消息@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;
}
}
例子:
{
"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"
}
什么是错的代码?我试图弄清楚,但我仍然没有提出任何事情。
你能分享的logcat例外的例子书面
"sent" : true
之前把@PropertyName("isSent")
?在你的数据库中是否有一个case'isSent'值不存在(因此是'null')? – Grimthorr请在发生异常的地方分享您的日志。 – Sarfaraz
请屏幕截图您的数据库控制台。领域'isSent'不存在 – faruk