0

I have previously searched for solutions to this but I'm having problems understanding exactly how I'm getting a NullPointerException. My goal is to set a message for a textView, but the app crashes when it happens. This is the error:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.okimin.flashchatnewfirebase.Post.getMessage()' on a null object reference

The variables:

private FirebaseUser user;
private FirebaseAuth mAuth;
private DatabaseReference mDatabaseReference;

Initializing them:

mAuth = FirebaseAuth.getInstance();
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
user = FirebaseAuth.getInstance().getCurrentUser();

Where the problem happens:

final String iD= user.getUid();

mDatabaseReference.child("messages").child(iD).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        Post postIt = new Post();
        postIt.setMessage(dataSnapshot.getValue(Post.class).getMessage()); // Where the problem happens
        mtextview.setText(postIt.getMessage());
    }
}

Post class:

public class Post {
    private String message;
    private String author;

    public Post(){

    }



    public Post(String message, String author) {
        this.message = message;
        this.author = author;
    }


    public String getMessage() {
        return message;
    }



    public String getAuthor() {
        return author;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

The structure:

structure

Everything works until I get an actual value.

Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51
  • 5
    Does `iD` match the child under `messages`? My guess is that it does not, because the child under messages looks like that push id, but Firebase auth user ids don't. – Doug Stevenson Nov 26 '18 at 22:59
  • I am actually not familar with that when searching through solutions. Can you elaborate more on the FirebaseAuth ids not matching the push ID? – Daniel Gaston Nov 27 '18 at 00:44
  • As mentioned by Doug, apparently his message recording routine is recording under a push ID rather than a user ID. Check your write routine to write to the user ID node. – Itapox Nov 27 '18 at 09:33
  • @DanielGaston do mark the answer as correct by clicking the tick mark looking button next to the answer, this helps future readers of the question and I'd appreciate that too. Cheers! :) – PradyumanDixit Nov 29 '18 at 09:15

1 Answers1

1

I think the same as @DougStevenson said, your iD does not matches with the one in the Firebase Database.

Try putting it on log using following lines, just before you add valueEventListener to it.

Log.i("seeThis",iD);

You can use the Firebase uid to set the Post class in your database and then your retrieving will also not be a problem.

What I am saying is that, you should set data in Firebase using a code like below:

DatabaseReference ref = FirebaseDatabse.getInstance().getReference().child("messages");

ref.child(user.getUid()).setValue(Post.class);
PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20