来自数据库的空对象引用

时间:2019-07-06 20:17:08

标签: android firebase-realtime-database

我想通过MessageActivity显示消息,但是应用程序不断崩溃,即使我更改代码,调试器也会向我显示相同的错误。 我的数据库如下所示: ![1] https://imgur.com/a/SWAxkWV

我尝试断言聊天!=空。

我从数据库中读取并在其中放置内容的MessageActivity。

public class MessageActivity extends AppCompatActivity {
    TextView username;
    FirebaseUser fuser;
    DatabaseReference reference;
    Intent intent;
    ImageButton btn_send;
    EditText text_send;
    MessageAdapter messageAdapter;
    List<Chat> mchat;
    RecyclerView recyclerView;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_message );
        btn_send=findViewById ( R.id.btn_send );
        text_send=findViewById ( R.id.editText );
        recyclerView=findViewById ( R.id.recycler_view );
        recyclerView.setHasFixedSize ( true );
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager ( getApplicationContext () );
        linearLayoutManager.setStackFromEnd ( true );
        recyclerView.setLayoutManager ( linearLayoutManager );







            username=findViewById ( R.id.username6 );


            intent=getIntent ();
            final String userid=intent.getStringExtra ( "FIRST NAME " );
        fuser= FirebaseAuth.getInstance ().getCurrentUser ();
        reference= FirebaseDatabase.getInstance ().getReference ("users");
        reference.addValueEventListener ( new ValueEventListener () {
            @Override
            public void onDataChange (@NonNull DataSnapshot dataSnapshot) {



                for(DataSnapshot snapshot:dataSnapshot.getChildren ()){
                    User user=snapshot.getValue (User.class);


                    assert user!=null;
                    assert fuser!=null;
                    if(!fuser.getUid ().equals ( user.getFirstName () )){
                        username.setText ( user.getFirstName () );



                    }

                }
                readMessages ( fuser.getUid (),userid );

            }

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

            }
        } );
        btn_send.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick (View view) {
                String msg=text_send.getText ().toString ();
                if(!msg.equals ( "" )){
                    sendMessage ( fuser.getUid (),userid,msg );
                } else{
                    Toast.makeText ( MessageActivity.this,"You can't send empty",Toast.LENGTH_SHORT ).show ();
                }
                text_send.setText ( "" );
            }

        } );


        }
private void sendMessage(String sender,String receiver,String message)
{
    DatabaseReference reference=FirebaseDatabase.getInstance ().getReference ();
    HashMap<String,Object> hashMap=new HashMap<> (  );
    hashMap.put ( "sender",sender );
    hashMap.put ( "receiver",receiver );
    hashMap.put ( "message",message );
    reference.child ( "Chats" ).push ().setValue ( hashMap );
}
private void readMessages(final String myid, final String userId)
{
    mchat=new ArrayList<> (  );
    reference=FirebaseDatabase.getInstance ().getReference ("Chats");
    reference.addValueEventListener ( new ValueEventListener () {
        @Override
        public void onDataChange (@NonNull DataSnapshot dataSnapshot) {
            mchat.clear ();
            for(DataSnapshot snapshot:dataSnapshot.getChildren ()){
                Chat chat=snapshot.getValue (Chat.class);

                assert chat != null;
                if(chat.equals ( chat.getReceiver ())&& !fuser.getUid ().equals ( chat.getSender ()) || !fuser.getUid ().equals ( chat.getSender () )&&!fuser.getUid ().equals ( chat.getReceiver () ) )  {
                    mchat.add ( chat );
                }
                messageAdapter=new MessageAdapter ( MessageActivity.this,mchat );
                recyclerView.setAdapter ( messageAdapter );
            }
        }

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

        }
    } );
}
    }

出现错误的地方:

private void readMessages(final String myid, final String userId)
{
    mchat=new ArrayList<> (  );
    reference=FirebaseDatabase.getInstance ().getReference ("Chats");
    reference.addValueEventListener ( new ValueEventListener () {
        @Override
        public void onDataChange (@NonNull DataSnapshot dataSnapshot) {
            mchat.clear ();
            for(DataSnapshot snapshot:dataSnapshot.getChildren ()){
                Chat chat=snapshot.getValue (Chat.class);

                assert chat != null;
                if(chat.getReceiver ().equals ( myid )&& chat.getSender ().equals ( userId ) || chat.getReceiver ().equals ( userId )&& chat.getSender ().equals ( myid )) {
                    mchat.add ( chat );
                }
                messageAdapter=new MessageAdapter ( MessageActivity.this,mchat );
                recyclerView.setAdapter ( messageAdapter );
            }
        }

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

        }
    } );
}

聊天活动:

public class Chat {
    private String sender;
    private String receiver;
    private String message;

    public Chat (String sender, String receiver, String message) {
        this.sender = sender;
        this.receiver = receiver;
        this.message = message;
    }
    public Chat(){

    }

    public String getSender () {
        return sender;
    }

    public void setSender (String sender) {
        this.sender = sender;
    }

    public String getReceiver () {
        return receiver;
    }

    public void setReceiver (String receiver) {
        this.receiver = receiver;
    }

    public String getMessage () {
        return message;
    }

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

调试器向我显示:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.sportsbuddy, PID: 8531
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at com.example.sportsbuddy.MessageActivity$3.onDataChange(MessageActivity.java:135)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@17.0.0:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@17.0.0:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@17.0.0:55)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:6986)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

我希望在数据库中显示来自接收者和发送者的消息。

0 个答案:

没有答案