Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6650045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:50:48+00:00 2026-05-26T00:50:48+00:00

When this ListView has more items then can be shown on the screen (you

  • 0

When this ListView has more items then can be shown on the screen (you have to scroll to see the others) and you select one of the items I get a NullPointerException

Here is the code:

studentList = (ListView)findViewById(R.id.studentListView);
getAllStudents();
studentList.setOnItemClickListener(new OnItemClickListener(){
     @Override
     public void onItemClick(AdapterView<?> a, View v, int position, long id) {

        for (int i = 0; i < studentList.getCount(); i++){

                if (i == position){
                    studentList.getChildAt(i).setBackgroundResource(R.color.selectedListViewBg);
                }
                else{
                    studentList.getChildAt(i).setBackgroundResource(R.color.listViewBg);
                }   

            }

To pull the students from the database and add them to the studentList ArrayAdapter:

getAllStudents(); 

I get the Exception at:

studentList.getChildAt(i).setBackgroundResource(R.color.<either one>);

If the ListView can hold all the items without having to scroll, you can select without the NullPointerException

What am I doing wrong?

EDIT :

After reading the answers to this question, I decided to change the logic at this part of my program.

I probably should have explained what I was trying to do better:

I have a ListView that is populated with students. There is also a button at the bottom that says “Next”. I wanted the user to be able to click on their choice and have the background color change. This section of code was supposed to change the selected item’s background to one color and all the other item’s backgrounds back to the original color.

My Solution:
I got rid of the button and move on to the next page when the user clicks on the ListView item.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:50:48+00:00Added an answer on May 26, 2026 at 12:50 am

    getChildAt() returns to you the child View of the ListView at the specified index in the screen. This will only encompass what is currently rendered (ListView children views are recycled), so it will always go from 0 to about 7 (for example). So if you have more than 8 students you’re trying to get the one at the 9th position, which does not exist.

    However, the problem with your code (if it were working) is that you would need to store the “selected” state internally in your model anyway, otherwise once you scroll the background color will go back to being the original one.

    I would suggest adding a checkbox widget and set that as checked/unchecked. You will still need to save this information in your model and set the checkbox state accordingly when recycling (in getView()) but it will make more sense in an Android environment than using the background color as a “selected indicator”.

    But to answer the original question, the easiest way to “fix” your code is to make use of the View argument that you’re getting (the v).

    for (int i = 0; i < studentList.getChildCount(); i ++) {
      studentList.getChildAt(i).setBackground(unselected);
    }
    v.setBackground(selected);
    

    But again, you will notice this will not really work as soon as you scroll.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an android listview filled with items. Every item has a button. This
I have a ListView which can contain any number of items. When it uses
ListView has this nice feature where you can set an overscroll header or footer.
I have an ASP.NET 2.0 ListView control (aka:parent) and configured inside this ListView I
This is a very common scenario: displaying images in a ListView which have to
I saw this signature on the ListView class: public ListView..::.ListViewItemCollection Items { get; }
I have a ListView setup in details mode that looks like this: When the
I have a ListView control on my form set up like this in details
I have a an Android ListView that has small (say, 1-5 frame) stutters as
I have a activity which showing the 20 items in listview, at bottom list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.