0

Possible solution, but structure failureIn my app there is a list I want to retrieve from Firebase Realtime Database, but at the second I add something on the database the list on the app does not actualize, it crashes. Whats wrong with the ValueListener?

  mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for (DataSnapshot ds: dataSnapshot.getChildren()){

                    comment = ds.getValue(Comment.class);
                    list.add(comment.getCommentText().toString()+" -"+comment.getUser().toString());
                }

                listView.setAdapter(adapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference at world.cryneo.partytime.java.EventProfileActivity$1.onDataChange(EventProfileActivity.java:71)

After the app crashes and restarts the new comment from the database is shown. But firstly it crashes.

This is what is getting uploaded. After upload the app crashs, but after restart of app the content is normally shown. So why crash?

 public void uploadComment(){

        String nextIndex = Long.toString(eventCommentsCounter+1);
        newComment = input_event_comment.getEditText().getText().toString();
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Events").child(event_name).child("comments").child(nextIndex).child("user");
        mDatabase.setValue(userName);
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Events").child(event_name).child("comments").child(nextIndex).child("commentText");
        mDatabase.setValue(newComment);
        finish();

        Intent eventIntent = new Intent(WriteCommentActivity.this, EventProfileActivity.class);
        eventIntent.putExtra("Event_Name", event_name);
        startActivity(eventIntent);
        finish();

    }
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
FlazzerXP
  • 75
  • 2
  • 8
  • Can you show what you're pushing to the database, or the code you're using to do that? seems like your comment object might be empty – zbys Aug 02 '18 at 15:34
  • At which line of code are you getting that error? At this `input_event_comment.getEditText().getText().toString()`? Please responde with @AlexMamo – Alex Mamo Aug 02 '18 at 15:47
  • list.add(comment.getCommentText().toString()+" -"+comment.getUser().toString()); @AlexMamo – FlazzerXP Aug 02 '18 at 15:50

1 Answers1

1

Seems like you're adding values to the database one at a time, so when you add one of the values like "user", your listener activates and builds an imcomplete Comment object that has no commentText.

Try building your POJO before and uploading all the Comment data at once, instead of by field.

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference index = database.getReference("Events").child(event_name).child("comments").child(nextIndex);

        index.push().setValue(new Comment(user, commentText));
zbys
  • 453
  • 2
  • 8
  • Yes you are right, that might be the solution, but how can I add all Comment data at one? And sry what is POJO? – FlazzerXP Aug 02 '18 at 15:44
  • Plain old java object, it's what "Comment" is. I edited my post and added an example to go by where you push the value which is a comment object you create – zbys Aug 02 '18 at 15:47
  • Good idea, its near finished, I added a foto, the problem is now that the structure is not good anymore, look at it, there is "-LIvNOXKg127..." This needs to go away as its like number 13. Number 14 is the new one with your code. – FlazzerXP Aug 02 '18 at 16:09
  • https://i.stack.imgur.com/OCq7M.jpg – FlazzerXP Aug 02 '18 at 16:10
  • Any reason you need indexes to be number (13, 14..)? Couldn't you just skip the next index and push the object directly with the random keys like -LlvNOXkg127.. – zbys Aug 02 '18 at 16:51
  • 1
    Yes you are right. Indeed there is no reason why the indexes must be numbers. Thanks for your help, everythink works now :) – FlazzerXP Aug 02 '18 at 17:22
  • it's my pleasure! :) – zbys Aug 02 '18 at 17:23