-1

As an old C++ programmer I am struggling with some C# issues. I want to reduce the redundancy in setting up a ListView. So I tried the code below. I get a null reference error, but I do not understand why. The compiler has no problem with me creating an array of ListViewItems, but I don't see how to use them.

Thanks, Russ


    ListViewItem [] items = new ListViewItem [12];
    for (int i=0; i < 12; ++i) {
        items [i].Text = string.Format ("F{0}", i+1);
   }
RussK2txb
  • 9
  • 4
  • 3
    As an old C++ programmer, I never expected new array of pointers to be initialized with anything but nulls. Think of c# references as like c++ references, but you can rebind them to something else: Sort of a hybrid of pointer and reference. You aren't creating an array of all ListViewItem. You're creating an array *rebindable references* that will refer to ListViewItems just as soon as you initialize them. – 15ee8f99-57ff-4f92-890c-b56153 Sep 13 '19 at 17:02
  • Where are you getting the null reference error? Is this the complete code? Can you paste both the error and which line of the above gives that error? Also, initializing the array is not the same thing as initializing a `ListViewItem`; you want to create a new ListViewItem and add it to the array. I'd also recommend using the `List` instead of an Array of `ListViewItem`. – George Stocker Sep 13 '19 at 17:03
  • 4
    You're allocating storage for an array that will hold 12 items of type `ListViewItem`. You're not creating 12 instances of `ListViewItem`. You have to initialize the elements of the array after allocating it. – Ken White Sep 13 '19 at 17:03
  • 1
    @KenWhite He's allocating storage for twelve *references to* ListViewItem. Whether this is winforms or WPF, it's a reference type. – 15ee8f99-57ff-4f92-890c-b56153 Sep 13 '19 at 17:05
  • 1
    for each item _new ListViewItem(); Item.Text = ..._ - to explain @Ken White 's comment – Daniel Schmid Sep 13 '19 at 17:07
  • @EdPlunkett: Yep. That's what I said (in less formal phrasing). :-) – Ken White Sep 13 '19 at 18:16
  • @KenWhite Given that OP's confusion is due to the mistaken belief that the array holds 12 actual items of type ListViewItem, I still think it's worth noting that it doesn't. – 15ee8f99-57ff-4f92-890c-b56153 Sep 13 '19 at 18:24
  • @EdPlunkett: I also said that, with my second sentence, and continued with my third to explain further what was needed to correct the problem. In fact, I said just what George said in the first sentence of his answer below in my first two sentences. Not sure what part you're taking issue with here. – Ken White Sep 13 '19 at 18:35

2 Answers2

0

You're allocating memory for an Array of 12 ListViewItems, but you haven't created the ListViewItem:

Instead, try something like the following:

List<ListViewItem> items = new List<ListViewItem>();
for (int i = 0; i < 12; i++) {
  items.Add(  //using the List.Add() method to add an item
    new ListViewItem 
    { 
      Text = string.Format ("F{0}", i+1); //Object Initialization syntax to add an item after construction
    });
}

This will create a List (an expandable collection); create a new ListViewItem in a loop, give that ListViewItem the text you want it to have, and then add that item to the List.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
0

It seems that you're receiving the null reference because you're trying to set a property(Text) on something that wasn't initialized (ListViewItem[i]). Try this:

ListViewItem [] items = new ListViewItem [12];
    for (var i = 0; i < items.Length; ++i) {
        items[i] = new ListViewItem{Text = string.Format ("F{0}", i+1)};
}