Home > Software engineering >  NullPointerException on my app its keep crushing android studio
NullPointerException on my app its keep crushing android studio

Time:03-15

I am using firestore in my app and when I run it, it keeps crashing.

Code:

RecyclerView RecyclerView;
ArrayList<Sabah> sabahArrayList;
Myadapter myadapter;
FirebaseFirestore db;
ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_azkarsabah);

    progressDialog = new ProgressDialog(this);
    progressDialog.setCancelable(false);
    progressDialog.setMessage("Fetching Data...");
    progressDialog.show();


    RecyclerView = findViewById(R.id.recyclerview);
    RecyclerView.setHasFixedSize(true);
    RecyclerView.setLayoutManager(new LinearLayoutManager(this));


    db = FirebaseFirestore.getInstance();
    sabahArrayList = new ArrayList<Sabah>();
    myadapter = new Myadapter(azkarsabah.this,sabahArrayList);

    RecyclerView.setAdapter(myadapter);

    EventChangeListener();

}

private void EventChangeListener() {

    db.collection("Sabah").orderBy("sabahzekarnum", Query.Direction.ASCENDING)
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {

            if (error != null){

                if(progressDialog.isShowing())
                    progressDialog.dismiss();

                Log.e("Error" , error.getMessage());
                return;
            }

            for (DocumentChange dc : value.getDocumentChanges()){

                if (dc.getType() == DocumentChange.Type.ADDED){

                    sabahArrayList.add(dc.getDocument().toObject(Sabah.class));


                }

                myadapter.notifyDataSetChanged();
                if(progressDialog.isShowing())
                    progressDialog.dismiss();
            }

        }
    });
}
}

and this is my adapter

public class Myadapter extends RecyclerView.Adapter<Myadapter.MyViewHolder> {

Context context;
ArrayList<Sabah> sabahArrayList;

public Myadapter(Context context, ArrayList<Sabah> sabahArrayList) {
    this.context = context;
    this.sabahArrayList = sabahArrayList;
}

@NonNull
@Override
public Myadapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(context).inflate(R.layout.activity_azkarsabah,parent,false);

    return new MyViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull Myadapter.MyViewHolder holder, int position) {

    Sabah sabah = sabahArrayList.get(position);

    holder.sabahzekershow.setText(sabah.sabahzekershow);
    holder.sabahzekarnum.setText(String.valueOf(sabah.sabahzekarnum));


}

@Override
public int getItemCount() {
    return sabahArrayList.size();
}

public static class MyViewHolder extends RecyclerView.ViewHolder{

    TextView sabahzekarnum,sabahzekershow;


    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        sabahzekarnum = itemView.findViewById(R.id.sabahzekarnum);
        sabahzekershow = itemView.findViewById(R.id.sabahzekershow);
    }
}

}

In logcat it give me this problems. For Whitelisted problem I tried to free my phone space since I use phone while running the app. I also cleaned the project but I still have the same problem and other problems. I did not understand what the problem is.

  2022-03-15 08:27:15.140 9732-9732/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2022-03-15 08:27:15.142 9732-9732/? E/Zygote: accessInfo : 1


    2022-03-15 08:27:18.087 9732-9732/com.example.tasbeeh E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.tasbeeh, PID: 9732
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.tasbeeh.Myadapter.onBindViewHolder(Myadapter.java:38)
        at com.example.tasbeeh.Myadapter.onBindViewHolder(Myadapter.java:14)
        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.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:22415)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
        at android.view.View.layout(View.java:22415)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:22415)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:530)
        at android.view.View.layout(View.java:22415)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:22415)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
        at android.view.View.layout(View.java:22415)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:1083)
        at android.view.View.layout(View.java:22415)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3389)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2856)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1904)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8535)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
        at android.view.Choreographer.doCallbacks(Choreographer.java:761)
        at android.view.Choreographer.doFrame(Choreographer.java:696)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
        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:7127)
        at java.lang.reflect.Method.invoke(Native Method)
2022-03-15 08:27:18.088 9732-9732/com.example.tasbeeh E/AndroidRuntime:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)

CodePudding user response:

Maybe you are inflating wrong layout in the RecyclerViewAdapter.

public Myadapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(context).inflate(R.layout.activity_azkarsabah,parent,false);

    return new MyViewHolder(v);
}

Here in LayoutInflater.from(context).inflate(R.layout.activity_azkarsabah,parent,false), you are trying to inflate R.layout.activity_azkarsabah but according to me you supposed to inflate Item View layout like R.layout.item_view.

Maybe I am wrong as I haven't see your activity_azkarsabah.xml file.

CodePudding user response:

Problem seems in MyAdapter You are set data on textview

holder.sabahzekershow.setText(sabah.sabahzekershow); holder.sabahzekarnum.setText(String.valueOf(sabah.sabahzekarnum));

First you check sabah.sabahzekershow OR sabah.sabahzekarnum in this two anythine one is null so your app is crash

CodePudding user response:

Problem seems in MyAdapter You are inflating layout of an activity

    View v = LayoutInflater.from(context).inflate(R.layout.activity_azkarsabah,parent,false);

But you should make another layout for RecyclerView items

Make another xml layout like item_myadapter Then

View v = LayoutInflater.from(context).inflate(R.layout.item_myadapter,parent,false);

Make sure to have textview in this layout with same id as

sabahzekarnum = itemView.findViewById(R.id.sabahzekarnum);
sabahzekershow = itemView.findViewById(R.id.sabahzekershow);

Layout item_myadapter looks like

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
    android:id="@ id/sabahzekarnum"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@ id/sabahzekershow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

If the above didn't helps then you may be getting NULL values in your list then try

holder.sabahzekershow.setText("" sabah.sabahzekershow);
holder.sabahzekarnum.setText("" String.valueOf(sabah.sabahzekarnum));

CodePudding user response:

Just Put One Condition When You Are Show Your Details As If Else, If Data Is Null Then Show Whatever And If Data Is Not Null Then Show Data

if (sabah.sabahzekershow == null){
    holder.sabahzekershow.setText("");
}else {
    holder.sabahzekershow.setText(sabah.sabahzekershow);
}
if (sabah.sabahzekarnum == null){
    holder.sabahzekarnum.setText("");
}else {
    holder.sabahzekarnum.setText(String.valueOf(sabah.sabahzekarnum));
}

Using This If Else Condition If Data Is Null But Your App Is Not Crash

  • Related