Currently I am making an app in Android which is a todo list, it has a edit text, button and a listview, when button is clicked the text is added to listview, everything works fine but when i add too many items to list view and try to select a list item my app stops working and I get a error saying
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
Does anyone know a fix?
this is my view data method whcih displays items in listview
public void viewdata(String tablename)
{
db = new mydbhandler(this);
lvitemslist = findViewById(R.id.lvitemlist);
ArrayList mylist = new ArrayList();
Cursor c = db.getdata(tablename);
while(c.moveToNext())
{
mylist.add(c.getString(1));
}
ListAdapter mylistadapter = new ArrayAdapter(this,R.layout.custom_item_row,R.id.ctv,mylist);
lvitemslist.setAdapter(mylistadapter);
}
and this is the onitemclicklistener
lvitemslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
View v = lvitemslist.getChildAt(i);
CheckedTextView ctv = v.findViewById(R.id.ctv);
ctv.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
db.deletedata(tablename,ctv.getText().toString());
viewdata(tablename);