shikher.mishra shikher.mishra - 3 months ago 24
Android Question

Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

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 list view 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.

public class HomePage extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {

ListView listView=(ListView)findViewById(;

DBhelperClass obj1=new DBhelperClass(this);
Cursor f=obj1.display();

AdapterForHomePage obj=new AdapterForHomePage(this,f,0);

} is a custom cursor adapter for populating the list view.)

public class AdapterForHomePage extends CursorAdapter {

LayoutInflater inflater;

public AdapterForHomePage(Context context, Cursor c, int i) {
super(context, c);

public View newView(Context context, Cursor cursor, ViewGroup parent) {

View v= inflater.inflate(R.layout.homepagelist,parent,false);
return v;


public void bindView(View view, Context context, Cursor cursor) {

TextView name=(TextView)view.findViewById(;
TextView emailId=(TextView)view.findViewById(;
// ImageView phoneCall=(ImageView)view.findViewById(;
TextView phoneNumber=(TextView)view.findViewById(;

String n=cursor.getString(cursor.getColumnIndex("NAME"));
String e=cursor.getString(cursor.getColumnIndex("EMAILID"));
String p=cursor.getString(cursor.getColumnIndex("PHONENUMBER"));



My DBhelper

This is the display function which is called in the 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.


You must call setContentView before you make any reference to a View inside a layout.


setContentView(R.layout.your_layout) creates the reference to your layout. Without it, your Activity doesn't know WHICH layout you're in, so it doesn't know the view (in your case, your ListView).

How do I solve it?

You have two options:

  1. Call on setContentView(R.layout.yourLayout) before ListView listView=(ListView)findViewById(;
  2. If you don't want to set the layout yet, you can use a LayoutInflater like so:

    View v = getActivity().getLayoutInflater().inflate(R.layout.yourLayout,null);

    And then when you want to get your ListView, use v.findViewById instead of findViewById