【问题标题】:Firebase Database NullPointerException when using getValue(Boolean.class) [duplicate]使用 getValue(Boolean.class) 时的 Firebase 数据库 NullPointerException [重复]
【发布时间】:2017-10-12 16:06:46
【问题描述】:

我使用 Firebase 数据库并使用一个布尔字段将消息对象写入其中。当我尝试使用getValue(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 异常的例子吗?您的数据库中是否曾经出现过 isSent 值不存在的情况(因此是 null)?
  • 请在您发现异常的地方分享您的日志。
  • 请截屏您的数据库控制台。 isSent 字段可能不存在

标签: java android firebase firebase-realtime-database


【解决方案1】:

尝试将 isSent 变量的类型更改为 Boolean

如果dataSnapshot.child("isSent").getValue(Boolean.class)返回null,这将有助于在setSent(boolean)null调用方法中导致异常原因。

【讨论】:

    【解决方案2】:

    我使用的是布尔值,从来都不是问题。但是我曾经遇到过同样的问题,这是因为它在数据库中保存的字段为sent 而不是isSent。 你能从你的控制台截屏你的数据库吗?

    我的解决方案是将 @PropertyName("isSent") 放在你的 getter 和 setter 之前

    @PropertyName("isSent")
    public boolean isSent() {
    
        return isSent;
    }
    
    @PropertyName("isSent")
    public void setSent(boolean sent) {
    
        isSent = sent;
    }
    

    恐怕你数据库中的字段写的是"sent" : true而不是"isSent" : true

    【讨论】:

      【解决方案3】:

      尝试替换

      public void setSent(boolean sent) {
      
          isSent = sent;
      }
      

      public void setSent(boolean sent) {
      
          this.isSent = sent;
      }
      

      【讨论】:

        【解决方案4】:

        在我看来,布尔值(原语)和布尔值(类)之间似乎不匹配。你想解决这个问题并再试一次吗? 将消息模型类中的所有原始声明替换为布尔值(类)。 告诉我进展如何。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-21
          • 2019-07-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多