android – Firebase data is not getting displayed and getting error-ThrowExceptions

Exception or error:

When I searched anything it did nothing but when I press button my app get crash and getting error. I don’t know know what is wrong.

Main Activity

public class CriminalRecords extends Fragment {

    private static final String TAG = "CriminalRecords";


    private EditText msearchfield;
    private ImageButton msearchbtn;
    private FloatingActionButton mFab;

    private RecyclerView mResultList;

    private DatabaseReference mCriminalDatabase;



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

        msearchfield=view.findViewById(R.id.SearchCriminal);
        msearchbtn = view.findViewById(R.id.SearchButton);
        mFab = view.findViewById(R.id.AddCriminals);
        mResultList = view.findViewById(R.id.Criminal_result_list);
        mResultList.setHasFixedSize(true);
        mResultList.setLayoutManager(new LinearLayoutManager(getContext()));

        mCriminalDatabase = FirebaseDatabase.getInstance().getReference("Criminals");

        mFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent =new Intent(getContext(), Add_Criminals.class);
                startActivity(intent);

            }
        });

        msearchbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String searchText = msearchfield.getText().toString();
                firebaseSearch(searchText);

            }
        });

        return view;
    }

    private void firebaseSearch(String searchText) {
        Query firebaseSearchQuery = mCriminalDatabase.orderByChild("name").startAt(searchText).endAt(searchText + "\uf8ff");

        Log.d(TAG, "firebaseSearch: searching");

        FirebaseRecyclerOptions<Criminalupload>options=new FirebaseRecyclerOptions.Builder<Criminalupload>()
                .setQuery(firebaseSearchQuery,Criminalupload.class)
                .build();

        FirebaseRecyclerAdapter<Criminalupload, CriminalViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Criminalupload, CriminalViewHolder>(options)
        {

            @Override
            protected void onBindViewHolder(@NonNull CriminalViewHolder holder, int position, @NonNull Criminalupload model) {
                Log.d(TAG, "onBindViewHolder: BindView Active");
                holder.setDetails(getActivity().getApplicationContext(),model.getName(),model.getImageid(),model.getAddress1(),model.getAddress2(),model.getAddress3(),model.getMobnum());
                Log.d(TAG, "onBindViewHolder: Bindview done");
            }

            @NonNull
            @Override
            public CriminalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return null;
            }
        };

        firebaseRecyclerAdapter.startListening();

        Log.d(TAG, "firebaseSearch: setting adapter");
        mResultList.setAdapter(firebaseRecyclerAdapter);
        Log.d(TAG, "firebaseSearch: addapter set");

    }

    //View Holder Class
    public static class CriminalViewHolder extends RecyclerView.ViewHolder{

        View mView;
        public CriminalViewHolder(View itemView) {
            super(itemView);

            mView = itemView;
        }

        public void setDetails(Context ctx,String CName, String Cimg, String Addres1, String Addres2, String Addres3, Long Mob){
            Log.d(TAG, "setDetails: setting details");
            TextView cname = (TextView)mView.findViewById(R.id.cProfileName);
            TextView caddress1 = (TextView)mView.findViewById(R.id.CriminalAddress);
            TextView caddress2 = (TextView)mView.findViewById(R.id.CriminalAddress2);
            TextView caddress3 = (TextView)mView.findViewById(R.id.CriminalAddress3);
            TextView cmobile = (TextView)mView.findViewById(R.id.CriminalMobile);
            CircleImageView cprof = (CircleImageView)mView.findViewById(R.id.cProfileImg);

            cname.setText(CName);
            caddress1.setText(Addres1);
            caddress2.setText(Addres2);
            caddress3.setText(Addres3);
            cmobile.setText(Math.toIntExact(Mob));
            Glide.with(ctx).load(Cimg).into(cprof);

        }

    }

}

Error

2020-02-22 19:57:17.804 15653-15653/com.example.crimerecords D/AndroidRuntime: Shutting down VM
2020-02-22 19:57:17.805 15653-15653/com.example.crimerecords E/AndroidRuntime:

FATAL EXCEPTION: main
Process: com.example.crimerecords, PID: 15653
java.lang.NullPointerException: Attempt to read from field ‘android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView’ on a null object reference
at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7079)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
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:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:693)
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:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
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:858)

2020-02-22 19:57:17.820 15653-15653/com.example.crimerecords I/Process: Sending signal. PID: 15653 SIG: 9

How to solve:

Your onCreateViewHolder method returns null, while it should return a new view holder for the data from the database.

A typical implementation would look something like this:

public CriminalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.criminal, parent, false);

    return new CriminalViewHolder(view);
}

Where R.layout.criminal is the layout that you want to use to show a node from the database.

This is all quite well covered in the FirebaseUI documentation on using the FirebaseRecyclerAdapter, so I recommend spending some time reading that.

Leave a Reply

Your email address will not be published. Required fields are marked *