0

I programmed for contact list adding and updating Programme is given below

public class RecyclerContactAdapter extends RecyclerView.Adapter<RecyclerContactAdapter.ViewHolder> {
    Context context;
    ArrayList<ContactModel> arrContacts;


    RecyclerContactAdapter(Context Context, ArrayList<ContactModel> ArrContacts){
        this.context = context;
        this.arrContacts = arrContacts;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.contact_view, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.imgContact.setImageResource(arrContacts.get(position).img);
        holder.txtName.setText(arrContacts.get(position).name);
        holder.txtNumber.setText(arrContacts.get(position).number);

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView txtName, txtNumber;
        ImageView imgContact;
        public ViewHolder(@NonNull View Viewitem){
            super(Viewitem);
            txtName = Viewitem.findViewById(R.id.contact);
            txtNumber = Viewitem.findViewById(R.id.contactNumber);
            imgContact = Viewitem.findViewById(R.id.imgContact);

        }
    }
}

in logcat show java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference. and not run on emulator when i run app is crashed what i do pease suggest. at com.jkapp.myapplication.RecyclerContactAdapter.getItemCount(RecyclerContactAdapter.java:40)

public class MainActivity extends AppCompatActivity {
    ArrayList<ContactModel> arrContacts = new ArrayList<ContactModel>();
    RecyclerContactAdapter adapter;
    FloatingActionButton btnOpenDilog;
    RecyclerView recyclerView;

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

        RecyclerView recyclerView = findViewById(R.id.recyclerContact);
        btnOpenDilog = findViewById(R.id.btnOpenDilog);
        btnOpenDilog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                Dialog dialog = new Dialog(MainActivity.this);
                dialog.setContentView(R.layout.add_update_lay);
                EditText edtName = dialog.findViewById(R.id.edtName);
                EditText edtNumber = dialog.findViewById(R.id.edtNumber);
                Button btnAction = dialog.findViewById(R.id.btnOpenDilog);
                btnAction.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String name = "", number = "";

                        if (!edtName.getText().toString().equals("")) {
                               name = edtName.getText().toString();
                            }
                        else {
                            Toast.makeText(MainActivity.this, "Please Enter Contact Name",         
                             Toast.LENGTH_SHORT);
                        }
                        if (!edtNumber.getText().toString().equals("")) {
                            number = edtNumber.getText().toString();
                        }
                        else {
                            Toast.makeText(MainActivity.this, "Please Enter Contact Number",     
                            Toast.LENGTH_SHORT);
                        }
                        arrContacts.add(new ContactModel(name, number));
                        adapter.notifyItemInserted(arrContacts.size()-1);
                        recyclerView.scrollToPosition(arrContacts.size()-1);
                    }
                });
                dialog.show();

            };
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        arrContacts.add(new ContactModel(R.drawable.contact_svgrepo_com, "A", "9097887662"));
        arrContacts.add(new ContactModel(R.drawable.contact_svgrepo_com, "B", "9097887662"));
        arrContacts.add(new ContactModel(R.drawable.contact_svgrepo_com, "C", "9097887662"));
        arrContacts.add(new ContactModel(R.drawable.contact_svgrepo_com, "D", "9097887662"));
        arrContacts.add(new ContactModel(R.drawable.contact_svgrepo_com, "E", "9097887662"));
        arrContacts.add(new ContactModel(R.drawable.contact_svgrepo_com, "F", "9097887662"));
        arrContacts.add(new ContactModel(R.drawable.contact_svgrepo_com, "G", "9097887662"));
        arrContacts.add(new ContactModel(R.drawable.contact_svgrepo_com, "H", "9097887662"));
        RecyclerContactAdapter adapter = new RecyclerContactAdapter(this,arrContacts);
        recyclerView.setAdapter(adapter);
    }
}
pfurbacher
  • 1,789
  • 3
  • 15
  • 23

0 Answers0