shikher.mishra shikher.mishra - 1 month ago 9
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.

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 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);
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.

Answer

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


Why?

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(R.id.listUserDetails);
  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