0

I'm searching by key in my Firebase database, but it retrieves null and I don't know why.

data.getUrl() or data.getText() both give me null values.

In debugging, before line

test data = dataSnapshot.getValue(test.class);

it shows me that data should've retrieved what I'm looking for, but when that line executes, it gives me null.

Database structure:

{
  "images" : {
    "partner " : {
      "text" : "partner",
      "url" : "http://res.cloudinary.com/dg3jylcsw/image/upload/v1483565650/sister_x3lv6j.png"
    },
    "sister" : {
      "text " : "sister",
      "url" : "http://res.cloudinary.com/dg3jylcsw/image/upload/v1483565650/sister_x3lv6j.png"
    }
  }
}

test class :

package com.example.android.testy;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown=true)
public class test {
    private String text;
    private String url;

    public test() {
        // empty default constructor, necessary for Firebase to be able to deserialize blog posts
    }
    public String getText() {
        return text;
    }

    public String getUrl() {
        return url;
    }
}

Mainactivity:

package com.example.android.testy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.Query;
import com.firebase.client.ValueEventListener;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Firebase.setAndroidContext(this);

        Firebase rootRef = new Firebase("https://test-219f8.firebaseio.com/images");
        Query queryRef = rootRef.orderByKey().equalTo("sister");
        queryRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                    test data = dataSnapshot.getValue(test.class);

                    System.out.println(data.getUrl());

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("The read failed: " + firebaseError.getMessage());
            }
        });

    }


}
AL.
  • 36,815
  • 10
  • 142
  • 281
V.Fayez
  • 23
  • 1
  • 10

1 Answers1

1

you need to use addChildEventListener instead of addValueEventListener.

try this :

queryRef.addChildEventListener(new ChildEventListener() {
    public void onChildAdded(DataSnapshot dataSnapshot, String previousKey) {
        test data = dataSnapshot.getValue(test.class);

                    System.out.println(data.getUrl());
    }
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {
    }
    public void onChildRemoved(DataSnapshot dataSnapshot) {
    }
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {
    }
    public void onCancelled(FirebaseError firebaseError) { }
});
Saurabh
  • 1,225
  • 9
  • 16