我使用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