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();
}
}
}`
package com.example.ttt;
public class firebasemodel{
String name;
String image;
String uid;
static String status;
public firebasemodel(String name, String image, String uid, String status) {
this.name = name;
this.image = image;
this.uid = uid;
this.status = status;
}
public firebasemodel() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public static String getStatus()
{
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
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 :)
Updated: I think the answer could be simpler. In your statement, you use firebasemodel.getStatus(). Did you want to write firebaseModel.getStatus()?