从Firebase数据库检索数据时遇到问题

时间:2019-03-23 15:59:08

标签: android firebase-realtime-database

我从firebase.im检索数据时遇到问题,试图获取与此节点“ 0506326662”和子节点“ Location”有关的信息。 我还创建了一个名为UserInformation的类,该类会将数据作为对象保存,但是由于某种原因,我遇到了错误,并且不知道它来自何处以及原因。

 databaseReference.child("Users").child(numbeT).
                child("Location").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for(DataSnapshot ds:dataSnapshot.getChildren())
                {
                     uInfo=new UserInformation();
                     uInfo.setLastOnline(ds.child(numbeT).getValue(UserInformation.class).getLastOnline());
                    uInfo.setLag(ds.child(numbeT).getValue(UserInformation.class).getLag());
                    uInfo.setLat(ds.child(numbeT).getValue(UserInformation.class).getLat());

                }

https://ibb.co/6tXN5H9

错误LogCat(firebase)

2019-03-23 17:58:52.187 4657-4657/com.alrubaye.mytracker E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.alrubaye.mytracker, PID: 4657
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.text.SimpleDateFormat com.alrubaye.mytracker.UserInformation.getLastOnline()' on a null object reference
        at com.alrubaye.mytracker.MyServie$1.onDataChange(MyServie.java:79)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.1.0:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.1.0:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.1.0:55)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

UserInformation类:

package com.alrubaye.mytracker;

import java.text.SimpleDateFormat;

public class UserInformation {
    private SimpleDateFormat lastOnline;
    private String lag;
    private String lat;

    public UserInformation() {

    }

    public SimpleDateFormat getLastOnline() {
        return lastOnline;
    }

    public void setLastOnline(SimpleDateFormat lastOnline) {
        this.lastOnline = lastOnline;
    }

    public String getLag() {
        return lag;
    }

    public void setLag(String lag) {
        this.lag = lag;
    }

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }
}

0 个答案:

没有答案