0

How I can retrieve uid2

"myChat" : {
"LVaLUQ9eN8PmkazanpXzL7QdP573" : {
  "f4cTqaXf81R5dTI5AV5pMWLIjoB2" : {
    "Massige" : {
      "-KzyFATWHPh81MqAMiI7" : {
        "author" : "mohammed",
        "authorL" : "mohammed",
        "text" : "hi",
        "uid" : "LVaLUQ9eN8PmkazanpXzL7QdP573",
        "urlPhoto" : "https://firebasestorage.googleapis.com/v0/b/-acf07.appspot.com/o/LVaLUQ9eN8PmkazanpXzL7QdP573%2FmyImgebackgroun%2Fbaground?alt=media&token=744484a1-339b-4bc4-889a-019fb85854fa"
      },
    },
    "id" : {
      "uid2" : "mohammed"
    }
  }
},
"f4cTqaXf81R5dTI5AV5pMWLIjoB2" : {
  "LVaLUQ9eN8PmkazanpXzL7QdP573" : {
    "Massige" : {
      "-KzyFATbNXQLpQhIofau" : {
        "author" : "mohammed",
        "authorL" : "mohammed",
        "text" : "hi",
        "uid" : "LVaLUQ9eN8PmkazanpXzL7QdP573",
        "urlPhoto" : "https://firebasestorage.googleapis.com/v0/b/-acf07.appspot.com/o/LVaLUQ9eN8PmkazanpXzL7QdP573%2FmyImgebackgroun%2Fbaground?alt=media&token=744484a1-339b-4bc4-889a-019fb85854fa"
      },
    },
    "id" : {
      "uid2" : "mohammed"
    }
  }
 }
 },

This code which i was try to retrieve but not worked.

    final TextView text=(TextView)findViewById(R.id.textView26);

    reference = FirebaseDatabase.getInstance().getReference("myChat");
    mAuth = FirebaseAuth.getInstance();
    String currentUser = mAuth.getCurrentUser().getUid();


    reference.child(currentUser).child("id").addValueEventListener(new ValueEventListener() {


        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            final String name = dataSnapshot.child("uid2").getValue().toString();
            text.setText(name);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

error log

E/AndroidRuntime: FATAL EXCEPTION: main, PID: 9410 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference at at com.google.android.gms.internal.zzbpx.zza(Unknown Source) at com.google.android.gms.internal.zzbqx.zzZT(Unknown Source) at com.google.android.gms.internal.zzbra$1.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

  • What's the error? What went wrong when you were running the code? – Al0x Nov 28 '17 at 08:07
  • not showing the data –  Nov 28 '17 at 08:08
  • there is typo you should get child("id") as reference and retrieve what inside child("uid2") – HemalHerath Nov 28 '17 at 08:31
  • replace it with getValue or where –  Nov 28 '17 at 08:37
  • i dont undrstund when i should replace getchild –  Nov 28 '17 at 09:02
  • its not working until now –  Nov 28 '17 at 09:03
  • still getting the same error – HemalHerath Nov 28 '17 at 09:04
  • yes but iam trying to change in the code and json –  Nov 28 '17 at 09:08
  • thank you so much its work now –  Nov 28 '17 at 09:09
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Dec 13 '17 at 16:29
  • 2
    Please don't make more work for people by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the [CC BY-SA 3.0 license](https://creativecommons.org/licenses/by-sa/3.0), for SE to distribute that content (i.e. regardless of your future choices). By SE policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. – Makyen Dec 13 '17 at 16:30

2 Answers2

1

You can use something like below with helper class

ValueEventListener postListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // Get Post object and use the values to update the UI
        Post post = dataSnapshot.getValue(Post.class);
        // ...
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // Getting Post failed, log a message
        Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
        // ...
    }
};
mPostReference.addValueEventListener(postListener);

for more info : https://firebase.google.com/docs/database/android/read-and-write

Edit

Declare an instance (class variable)

private FirebaseAuth mAuth;
private DatabaseReference reference;

In onCreate() initialize mAuth and database

reference = FirebaseDatabase.getInstance().getReference().child("users");
mAuth = FirebaseAuth.getInstance();

get current user id to a string

FirebaseUser currentUser = mAuth.getCurrentUser();

and create a database reference for users like below

reference.child(currentUser).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                final String name = dataSnapshot.child("id").getValue().toString();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
halfer
  • 19,824
  • 17
  • 99
  • 186
HemalHerath
  • 1,006
  • 2
  • 18
  • 38
1

Try this , First your response covert into JsonObject and than parse it given below.

  try {
        JSONObject jsonObject = new JSONObject(result); // hear your responce
        JSONObject jsonObject1 = jsonObject.getJSONObject("LVaLUQ9eN8PmkazanpXzL7QdP573");
        JSONObject jsonObject2  = jsonObject1.getJSONObject("f4cTqaXf81R5dTI5AV5pMWLIjoB2");
        JSONObject jsonObject3 = jsonObject2.getString("id");
        String  uiod = jsonObject3.getString("uid2");

    }catch (JSONException j){
        j.printStackTrace();
    }
Payal Sorathiya
  • 756
  • 1
  • 8
  • 23
  • thanks but how i can retrive it with addValueEventListener or addChildEventListener –  Nov 28 '17 at 07:59
  • iam trying to retrieve it with listview –  Nov 28 '17 at 08:00
  • 2
    Sorathiya, would you add some text into your answer to explain what you have changed, so new readers can understand the solution more easily? – halfer Nov 28 '17 at 11:02