0

I have been suffering from this problem 2 days and i can't solve it

I have this code

public class SettingsActivity extends AppCompatActivity {
 private DatabaseReference mUserDatabase;
 private FirebaseUser mCurrentUser;
 //Android Layout

 private CircleImageView mDisplayImage;
 private TextView mName;
 private TextView mStatus;
 private Button mStatusBtn;
 private Button mImageBtn;
 private static final int GALLERY_PICK = 1;

 // Storage Firebase
 private ProgressDialog mProgressDialog;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_settings);

     mDisplayImage = (CircleImageView) findViewById(R.id.settings_image);
     mName = (TextView) findViewById(R.id.settings_name);
     mStatus = (TextView) findViewById(R.id.settings_status);
     mStatusBtn = (Button) findViewById(R.id.settings_status_btn);
     mImageBtn = (Button) findViewById(R.id.settings_image_btn);
     mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();

     String current_uid = mCurrentUser.getUid();
     mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);
     mUserDatabase.keepSynced(true);
     mUserDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String name = dataSnapshot.child("name").getValue().toString();
            final String image = dataSnapshot.child("image").getValue().toString();
            String status = dataSnapshot.child("status").getValue().toString();
            String thumb_image = dataSnapshot.child("thumb_image").getValue().toString();
            mName.setText(name);
            mStatus.setText(status);
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
 }
}

In my log i got this problem

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.pc.newchatj, PID: 724 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference at com.example.pc.newchatj.SettingsActivity$1.onDataChange(SettingsActivity.java:74) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:75) at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63) at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) 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:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Here is my FirebaseDatabase screen

enter image description here

Can any onw help please , Thanks !!!

13
  • 1
    This means that .getValue() on one of the properties inside the onDataChange function is returning null. Commented Oct 29, 2018 at 14:06
  • 1
    What does your firebase database structure look like? Commented Oct 29, 2018 at 14:06
  • 1
    Which line is line 74? Commented Oct 29, 2018 at 14:13
  • 1
    @TheWanderer There's a space after name, but yes that's what I figure should work aswell. Commented Oct 29, 2018 at 14:16
  • 1
    Yep the " in firebase variables are screwing up your data! Commented Oct 29, 2018 at 14:17

2 Answers 2

4

remove the " double quotes & _ spaces from Firebase field names name and status.

Sign up to request clarification or add additional context in comments.

9 Comments

Looks like there are manually added quotes in the values as well.
I don't think Firebase puts quotes around anything in the console display.
I'm also curious how OP is setting the data.If it's done programmatically, that code needs to be fixed.
@TheWanderer the quotes around string data are correct; just checked.
@TheWanderer field names and numeric values have no quotes there... strings do.
|
3

Adding to Martin answer, you can also do this

String name = dataSnapshot.child("name").getValue(String.class);

instead of this

String name = dataSnapshot.child("name").getValue().toString();

Also, always make sure that your reference is matching exactly your data at your Firebase Console and always use

if(dataSnapshot.exists()){....}

before getting any results, so you can catch any problems there

for example

    if(dataSnapshot.exists()){
    String name = dataSnapshot.child("name").getValue().toString();
                final String image = dataSnapshot.child("image").getValue().toString();
                String status = dataSnapshot.child("status").getValue().toString();
                String thumb_image = dataSnapshot.child("thumb_image").getValue().toString();
                mName.setText(name);
                mStatus.setText(status);

    }
  ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.