-2

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?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
shikher.mishra
  • 155
  • 8
  • 24

3 Answers3

1

you have not done setContentView(R.layout.yourlayout) on oncreate().

Mansi Salvi
  • 247
  • 1
  • 7
1

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

Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117
0

You need to call

setContentView(R.layout.you_activity_layout);

before making the reference of the ListView

Simone Masini
  • 154
  • 2
  • 5