【问题标题】:correct way of deleting items from firebase从firebase中删除项目的正确方法
【发布时间】:2020-05-15 08:50:54
【问题描述】:

我正在尝试使用 removeValue() 从我的数据库中删除项目。当我做我的应用程序崩溃时,因为 onChange 方法中显然有一个空指针。

从数据库中删除值以防止我的应用崩溃的最佳方法是什么?

这些是我要删除的值

 DatabaseReference detailref = bookingref.child("Bookings").child(customerid);
    detailref.child("Housenumber").removeValue();
    detailref.child("Address").removeValue();
    detailref.child("Postcode").removeValue();
    detailref.child("Phone").removeValue();
    detailref.child("Date").removeValue();

在另一个活动中,我使用值事件监听器调用它们。

public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.getChildrenCount()!=0){
                house = dataSnapshot.child("Housenumber").getValue().toString();
                address = dataSnapshot.child("Address").getValue().toString();
                postcode = dataSnapshot.child("Postcode").getValue().toString();
                phone = dataSnapshot.child("Phone").getValue().toString();
                date = dataSnapshot.child("Date").getValue().toString();

这是来自我的 Logcat 的空指针错误

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
    at studios.p9p.h20.showroomfinish_adminapp.Booking_details$4.onDataChange

【问题讨论】:

  • 如果请求的位置没有数据,getValue() 将返回 null。你需要检查一下。
  • 我知道原因,但我该如何检查呢?
  • 就像 Java 中的任何其他 null 检查一样。
  • 我还要指出,您在标题中的问题是关于如何删除内容,但您显示的代码都没有删除任何内容。您只显示了一些查询的处理,其结果丢失。在我看来,您可能确实删除了正确的内容,而您只是想检查它们是否真的被删除了?我不清楚。
  • 感谢您的帮助!

标签: java android firebase firebase-realtime-database nullpointerexception


【解决方案1】:

您能否详细说明您要做什么? 我认为它崩溃了,因为您要单独删除值。如果您尝试删除所有子数据,您可以尝试类似

 DatabaseReference detailref = bookingref.child("Bookings").child(customerid);
 detailref.removeValue();

如果没有,也许您可​​以在尝试获取值之前检查它是否存在... 不是最好或美丽的方式,但它可能会奏效......

public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  if(dataSnapshot.exists()
     {
        if (dataSnapshot.getChildrenCount()!=0){
            if(dataSnapshot.hasChild("Housenumber")
           {
            house = dataSnapshot.child("Housenumber").getValue().toString();
            }
            if(dataSnapshot.hasChild("Address")
            {
            address = dataSnapshot.child("Address").getValue().toString();
            }
            if(dataSnapshot.hasChild("Postcode")
            {
            postcode = dataSnapshot.child("Postcode").getValue().toString();
            }
            if(dataSnapshot.hasChild("Phone")
            {
            phone = dataSnapshot.child("Phone").getValue().toString();
            }
            if(dataSnapshot.hasChild("Date")
            {
            date = dataSnapshot.child("Date").getValue().toString();
            }
     } }

希望这会有所帮助:)

【讨论】:

  • 很好用,请参阅下面关于我如何使用它的答案。谢谢
【解决方案2】:

我使用了上面的答案,但方式更简单。我创建了一个名为 bookingModel() 的 firebase 模型,其中包含我所有的字符串,然后在我的 ondataChange() 中,我可以更改下面的所有笨重代码:)

house = dataSnapshot.child("Housenumber").getValue().toString();
address = dataSnapshot.child("Address").getValue().toString();
postcode = dataSnapshot.child("Postcode").getValue().toString();
phone = dataSnapshot.child("Phone").getValue().toString();
date = dataSnapshot.child("Date").getValue().toString();

id 已经获得了一个名为 customerId 的引用节点的用户 ID 字符串 所以我在我的 onDataChanged() 中使用了这个

if(dataSnapshot.hasChild(customerId))
bookingModel firebase_model = 
dataSnapshot.child(customerId).getValue(bookingModel.class)

然后删除我调用

detailRef.child(customerId).removeValue();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2018-10-30
    • 1970-01-01
    相关资源
    最近更新 更多