0

enter image description hereI'm retrieving some value form the RealTime database but every time it shows null pointer exception. actually i want to access all data from a child and also got new update on data when data changed via Database. My code is this, and in this code i want to append value in Text View but showing null pointer exception. please resolve my issue.

 db=FirebaseDatabase.getInstance();
        dr=db.getReference().child("us");

      dr.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    txtMsg.append((CharSequence) postSnapshot.child("name").getValue());
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

and my LogCat is:

0.481 31984-32018/com.thebhakti E/GED: Failed to get GED Log Buf, err(0)
09-02 16:51:11.891 31984-32036/com.thebhakti E/NativeCrypto: ssl=0xb7352a78 cert_verify_callback x509_store_ctx=0xa0aa11d0 arg=0x0
09-02 16:51:11.891 31984-32036/com.thebhakti E/NativeCrypto: ssl=0xb7352a78 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
09-02 16:51:14.579 31984-31984/com.thebhakti E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: com.thebhakti, PID: 31984
                                                               java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
                                                                   at android.widget.TextView.append(TextView.java:3643)
                                                                   at com.thebhakti.RealtimeMain$2.onDataChange(RealtimeMain.java:71)
                                                                   at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
                                                                   at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
                                                                   at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
                                                                   at android.os.Handler.handleCallback(Handler.java:815)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                   at android.os.Looper.loop(Looper.java:194)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5643)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Pradeep Sheoran
  • 493
  • 6
  • 15

2 Answers2

0

As you said that you want to update the data in TextView txtMsg when it is changed in the database. So the method you used will give following results which is not appropriate.

I am considering single name i.e. Akshay

txtMsg.append((CharSequence) postSnapshot.child("name").getValue());

It will give results - Akshay

Suppose you changed the name from Akshay to Punitt. So the name Punitt will be appended in txtMsg & the result will be-

AkshayPunitt

But the correct way would be that Akshay should be replaced by Punitt. So you should use the method setText() as shown-

txtMsg.setText(postSnapshot.child("name").getValue().toString());
Raj
  • 2,997
  • 2
  • 12
  • 30
0
This is not a proper way, use model class to retrieve a data.
please refer this link

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

Harsh Prajapati
  • 510
  • 5
  • 7