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

  • SEARCH
  • Home
  • 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 8928145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:22:11+00:00 2026-06-15T08:22:11+00:00

I have a problem that I can’t solve. When I add items to a

  • 0

I have a problem that I can’t solve.

When I add items to a my database, I try to populate my listView and remove any item that equals " ", which are 3 blank spaces, from the listView, and NOT from the database just so that the users can’t see the empty rows. The method load() is called to do so. I keep getting a nullPointerException. It seems to find the item (I tested it with a System.out.println()) but when the remove method is called it crashes.

private void load() {       
    Cursor c=helper.getById(taskId);
    c.moveToFirst();                
    String[] temp = getArray(helper.getPart(c));

    ArrayAdapter<String> arrayAdapter =      
             new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, temp);
    lv.setAdapter(arrayAdapter); 

    int count = arrayAdapter.getCount();
    for (int i = 0; i < count; i++) {
        if (arrayAdapter.getItem(i).equals("   ")) {
            arrayAdapter.remove(arrayAdapter.getItem(i));
        }                    
    }

    arrayAdapter.notifyDataSetChanged();

    c.close();
}
  • 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-06-15T08:22:14+00:00Added an answer on June 15, 2026 at 8:22 am

    Well it happens because the list is shrinking when you do a remove in your loop.

    Let’s say the size is 10 (so index 0 – 9) in the beginning, then when you remove the first item which matches your condition, the real size its going back to 9, but in your loop you still have “i < 10” when you try to access the index i = 9 then you have a NullPointer Exception.

    Try the following:

            for (int i = 0; i < arrayAdapter.getCount(); i++) {
                 if(arrayAdapter.getItem(i).equals("   ")) {
                     arrayAdapter.remove(arrayAdapter.getItem(i));
                 }                   
             }
    

    Note that I replaced your count variable with getCount() inside the loop. When you do it like this you should never have the NullPointer Exception.

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

Sidebar

Related Questions

i have a problem that i can't solve ! (sqlite3, but i think it
I have a strange problem that I can't seem to solve. I've quite a
I have a problem, that I can't solve. I am writing an application that
I have another problem that I can't solve I have a following code that
I have a problem that I can't solve... In my Activity, I instantiate a
I have a problem that i can't solve. The problem is as follows. CPP
Hi I have problem that I can't solve alone since damn debugging doesn't work
i have this problem that i can't solve for days now...here is my code.
I have a problem that I can't seem to solve. I have table: Software
I have a problem that I can't seem to solve. What I am trying

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.