简体   繁体   中英

My chat app keeps crashing, on android studio when i run it but there no build errors

as the title explains im creating a chat app on android studio using java and firebase however when ever i run the app it crashes it was fine at first but now im on the chat section of the code and when i try running it, it opens but then crashes there are no build errors. but the logcat shows this error:

2022-01-21 18:41:26.284 23468-23468/com.example.ttt E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ttt, PID: 23468
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at com.example.ttt.ChatFragment$1.onBindViewHolder(ChatFragment.java:63)
        at com.example.ttt.ChatFragment$1.onBindViewHolder(ChatFragment.java:53)
        at com.firebase.ui.firestore.FirestoreRecyclerAdapter.onBindViewHolder(FirestoreRecyclerAdapter.java:125)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1897)
        at androidx.recyclerview.widget.RecyclerView$1.run(RecyclerView.java:414)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1010)
        at android.view.Choreographer.doCallbacks(Choreographer.java:809)
        at android.view.Choreographer.doFrame(Choreographer.java:740)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:995)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8506)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
2022-01-21 18:41:26.324 23468-23468/com.example.ttt I/Process: Sending signal. PID: 23468 SIG: 9

Here is the java for chatfragment script:

 package com.example.ttt;

 import android.graphics.Color;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageView;
 import android.widget.TextView;
 import android.widget.Toast;

 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 import androidx.fragment.app.Fragment;
 import androidx.recyclerview.widget.LinearLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;

 import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
 import com.firebase.ui.firestore.FirestoreRecyclerOptions;
 import com.google.firebase.auth.FirebaseAuth;
 import com.google.firebase.firestore.FirebaseFirestore;
 import com.google.firebase.firestore.Query;
 import com.squareup.picasso.Picasso;

 public class ChatFragment extends Fragment
 {
     private FirebaseFirestore firebaseFirestore;
     private FirebaseAuth firebaseAuth;
     LinearLayoutManager linearLayoutManager;


ImageView mimageviewofuser;
FirestoreRecyclerAdapter<firebasemodel,NoteViewHolder> chatAdapter;

RecyclerView mrecyclerview;



@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
    View v=inflater.inflate(R.layout.chatfrag,container,false);

    firebaseAuth=FirebaseAuth.getInstance();
    firebaseFirestore=FirebaseFirestore.getInstance();
    mrecyclerview=v.findViewById(R.id.recyclerview);

    Query query=firebaseFirestore.collection("Users");
    FirestoreRecyclerOptions<firebasemodel> allusername=new FirestoreRecyclerOptions.Builder<firebasemodel>().setQuery(query,firebasemodel.class).build();

    chatAdapter=new FirestoreRecyclerAdapter<firebasemodel, NoteViewHolder>(allusername)
    {
        @Override
        protected void onBindViewHolder(@NonNull NoteViewHolder noteViewHolder, int i, @NonNull firebasemodel firebaseModel)
        {
            noteViewHolder.particularusername.setText(firebaseModel.getName());
            String uri=firebaseModel.getImage();



            Picasso.get().load(uri).into(mimageviewofuser);
            if (firebasemodel.getStatus().equals("Online"))
            {
                noteViewHolder.statusofuser.setText(firebaseModel.getStatus());
                noteViewHolder.statusofuser.setTextColor(Color.BLUE);
            }
            else
            {
                noteViewHolder.statusofuser.setText(firebaseModel.getStatus());
            }
            noteViewHolder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Toast.makeText(getActivity(), "Item is clicked", Toast.LENGTH_SHORT).show();
                }
            });


        }

        @NonNull
        @Override
        public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
        {
            View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.chatviewlayout,parent, false);
            return new NoteViewHolder(view);
        }
    };

    mrecyclerview.setHasFixedSize(true);
    linearLayoutManager=new LinearLayoutManager(getContext());
    linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
    mrecyclerview.setLayoutManager(linearLayoutManager);
    mrecyclerview.setAdapter(chatAdapter);

    return v;



}
public class NoteViewHolder extends RecyclerView.ViewHolder
{
    private TextView particularusername;
    private TextView statusofuser;

    public NoteViewHolder(@NonNull View itemView)
    {
        super(itemView);
        particularusername=itemView.findViewById(R.id.nameofuser);
        statusofuser=itemView.findViewById(R.id.statusofuser);
        mimageviewofuser=itemView.findViewById(R.id.imageviewofuser);




    }
}

@Override
public void onStart()
{
    super.onStart();
    chatAdapter.startListening();
}

@Override
public void onStop()
{
    super.onStop();
    if (chatAdapter!=null)
    {
        chatAdapter.stopListening();
    }
}

}`

I am not familiar with Java Android programming, but I would just remind you that just because a programs builds without error, it does not mean it won't crash later, due to a runtime error, when some parameter etc. violates certain assumptions. Exactly like in your trace (you had a Null Pointer exception - an object just wasn't there when you expected it).

A Null Pointer exception happens when:

Thrown when an application attempts to use null in a case where an object > is required. These include: Calling the instance method of a null object. Accessing or modifying the field of a null object. Taking the length of null as if it were an array. Accessing or modifying the slots of null as if it were an array. Throwing null as if it were a Throwable value.

From: https://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html

My tip for you is to familiarize yourself with your IDE's debugger ang workout the problem and think long and hard when one of those situations listed above might have happened.

I'm sorry that I can't help you specifically with Android.

I think in your statement in the onBindViewHolder() method:

if (firebasemodel.getStatus().equals("Online"))

firebasemodel.getStatus() returns null. After that, you try to call the equals() method on a null reference, as a result, a NullPointerException is thrown.

To avoid this, try replacing your code with the following:

if ("Online".equals(firebasemodel.getStatus()))

I recommend you to read about checked and unchecked exceptions to understand why the compiler doesn't show any errors during compilation of your code:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM