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 7842275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:16:59+00:00 2026-06-02T16:16:59+00:00

I’m trying to populate a ListView with an ArrayList> using a base adapter. The

  • 0

I’m trying to populate a ListView with an ArrayList> using a base adapter. The ArrayList is populated by a database, and it is possible for the database to have no entries, and thus a empty ArrayList, for example when the app is launched for the first time. While the ArrayList is empty, I receive a non de-script “java.lang.RuntimeException: Unable to start activity ComponentInfo … java.lang.NullPointerException”.

My onCreate method looks like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.samples_list_layout);
    mContext = getApplicationContext();
    lv = getListView();
    list = myDatabase.getInstance(mContext).getAllSamples();
    sa = new SamplesAdapter(this, list);
    lv.setAdapter(sa);
    registerForContextMenu(lv);
}

Setting lv.setAdapter(null) will get the app to display the empty list view I have set up. However, when I leave it up to the BaseAdapter, I get the error.
I’ve followed the 2 Android List8.java and List14.java examples, and either way give the same results.
My BaseAdapter class looks like this:

public class SamplesAdapter extends BaseAdapter {

private static final String SAMPLE_NAME_COL = "name";

private static final String SAMPLE_HOST_COL = "host";

private static final String SAMPLE_ICON_COL = "icon";

private static final String SAMPLE_MODIFIED_STAMP_COL = "moddate";

private Context mContext;
private ArrayList<HashMap<String, String>> samples = new ArrayList<HashMap<String, String>>();

public SamplesAdapter(Context c, ArrayList<HashMap<String, String>> list){
    mContext = c;
    samples = list;
}

public int getCount() {
    return samples.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.samples_row_layout, parent, false);
    TextView name = (TextView) v.findViewById(R.id.samples_name);
    name.setText(samples.get(position).get(SAMPLE_NAME_COL));
    TextView host = (TextView) v.findViewById(R.id.samples_host);
    host.setText(samples.get(position).get(SAMPLE_HOST_COL));
    TextView moddate = (TextView) v.findViewById(R.id.samples_mod_stamp);
    moddate.setText(samples.get(position).get(SAMPLE_MODIFIED_STAMP_COL));
    return v;
}

}

I should also note that the ListView properly displays items when there is something to show. It only fails when the ArrayList is empty. Also, I’m using Android 2.2 (Not the greatest, I know). Any help would be greatly appreciated.

  • 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-02T16:17:00+00:00Added an answer on June 2, 2026 at 4:17 pm

    let getCount return 0, in orded to avoid the NPE:

    public int getCount() {
        return (samples == null) ? 0 : samples.size();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.