Search code examples
javaandroidlistviewnullpointerexceptionlistadapter

NullPointerException occurred in parsing ListView item to setBackgroundResource


I have received a NullPointerException in my ListView item while calling setBackgroundResource on it.

Initially I've a list of items that display well and when user click on those list items basically it is OnClickListener, they will change the background color immediately. But what I wanted to do now is when I have to change the background color of some of the items onCreate but not waiting for users to click on the item. The line complained is on the line to setBackgroundResource().

Please kindly help me with this problem. Thank you. Below is my code.

    private ArrayList<String> initialfetch = new ArrayList<String>();
    private ListView mainListView;
    private ArrayAdapter<String> listAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listlayout); 

        mainListView = (ListView) findViewById( R.id.list );
        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, contactlist);
        mainListView.setAdapter(listAdapter);


       // get Intent from the previous calling activity
       Intent intent = getIntent();
       String identifier = intent.getStringExtra("identifier");
       String position = intent.getStringExtra("position");

       if(identifier.equals("edit"))
       { // model.Static_Activity.event_list is tested to pass the correct position of item into the list
        for(int i = 0; i < model.Static_Activity.event_list.get(Integer.parseInt(position)).getAttendees().size(); i++)
        {
            for(int j = 0; j < initialfetch.size(); j++)
            {
                if(initialfetch.get(j).equals(model.Static_Activity.event_list.get(Integer.parseInt(position)).getAttendees().get(i)))
                {   // Problems happened on this line
                    ((View) ((ListView) mainListView.getChildAt(j)).getAdapter()).setBackgroundResource(R.color.color_2);

                }
            }
        }
    }

}

$Edited Post: Below is my stack trace file and line 66 is the setBackGroundResource line:

    08-22 06:35:32.259: E/AndroidRuntime(1749): FATAL EXCEPTION: main
    08-22 06:35:32.259: E/AndroidRuntime(1749): java.lang.RuntimeException: Unable to start         activity ComponentInfo{com.example.socialeventplanner/view.Select_Attendees}:         java.lang.NullPointerException
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.app.ActivityThread.access$600(ActivityThread.java:141)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.os.Handler.dispatchMessage(Handler.java:99)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.os.Looper.loop(Looper.java:137)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.app.ActivityThread.main(ActivityThread.java:5041)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         java.lang.reflect.Method.invokeNative(Native Method)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         java.lang.reflect.Method.invoke(Method.java:511)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         dalvik.system.NativeStart.main(Native Method)
    08-22 06:35:32.259: E/AndroidRuntime(1749): Caused by: java.lang.NullPointerException
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         view.Select_Attendees.onCreate(Select_Attendees.java:66)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.app.Activity.performCreate(Activity.java:5104)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

Solution

  • You are calling getChildAt() on just created ListView, and it doesn't have children yet. Also note that if the user scrolls ListView up or down, its views are recycled so basically only the views exist that are in sight.

    Also even if you didn't get NPE you woud get ClassCastException when trying to convert ArrayAdapter returned from getAdapter() to View. getAdapter call is not needed here at all.

    What you really should do is to create a custom adapter:

    private class MyAdapter extends ArrayAdapter<String> {
        public MyAdapter() {
            super(MyActivity.this, R.layout.simplerow, contactlist);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
    
            if (some_condition)
                v.setBackgroundResource(R.color.color_2);
    
            return v;
        }
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        listAdapter = new MyAdapter();
        mainListView.setAdapter(listAdapter);
        ...
        // remove your setBackgroundResource code from here
    }