I am entering the details (Username and password) in the login page and then I am displaying all the details of all the users registered in the application in a ListView.
I am populating the ListView with an ImageView , username and password.
When I am redirecting the page from the Login page to the HomePage (in which I am having the ListView) I am getting this error of null exception.
Homepage.java
public class HomePage extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listView=(ListView)findViewById(R.id.listUserDetails);
DBhelperClass obj1=new DBhelperClass(this);
Cursor f=obj1.display();
AdapterForHomePage obj=new AdapterForHomePage(this,f,0);
listView.setAdapter(obj);
}
}
AdapterForHomePage.java (this is a custom CursorAdapter for populating the ListView).
public class AdapterForHomePage extends CursorAdapter {
LayoutInflater inflater;
public AdapterForHomePage(Context context, Cursor c, int i) {
super(context, c);
inflater=LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v= inflater.inflate(R.layout.homepagelist,parent,false);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView name=(TextView)view.findViewById(R.id.displayName);
TextView emailId=(TextView)view.findViewById(R.id.displayEmail);
// ImageView phoneCall=(ImageView)view.findViewById(R.id.displayCallImage);
TextView phoneNumber=(TextView)view.findViewById(R.id.displayPhone);
String n=cursor.getString(cursor.getColumnIndex("NAME"));
String e=cursor.getString(cursor.getColumnIndex("EMAILID"));
String p=cursor.getString(cursor.getColumnIndex("PHONENUMBER"));
name.setText(n);
emailId.setText(e);
phoneNumber.setText(p);
}
}
My DBhelper
This is the display function which is called in the HomePage.java class
public Cursor display()
{
SQLiteDatabase db=this.getReadableDatabase();
Cursor cs=db.rawQuery("SELECT * FROM USERINFO",null);
return cs;
}
The error which I am getting is :
Java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.hsports.bandpop/com.example.hsports.bandpop.HomePage} java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
I am getting the error in the CustomAdapter object.
How can I resolve this problem?