gpt4 book ai didi

java - 使用 getValue(Boolean.class) 时的 Firebase 数据库 NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-03 12:27:13 25 4
gpt4 key购买 nike

<分区>

我使用 Firebase 数据库并使用一个 boolean 字段将消息对象写入其中。当我尝试使用 getValue(Boolean.class) 读取该对象时,出现异常。只有在获取此 boolean 值时才会发生,我可以毫无问题地获取字符串。

导致异常的方法:

@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"
}

这段代码有什么问题?我试图弄清楚,但我仍然没有想出任何办法。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com