Thread: Issue with ListView Control

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    Issue with ListView Control

    This is driving me crazy. I have a listview control on form in VC# 2008 Express.
    There are three colums set in its properties and its in details mode. I'm trying to add rows of data in at run time but any time I try and access the list Items (either to Add or Clear) I'm told that I have a NullReferenceException and that "Object reference not set to an instance of an object."

    What am I doing wrong?

    Code:
    ListViewItem item = new ListViewItem("NewItem");
    item.SubItems.AddRange( new string[] { "SubItem1", "SubItem2" });
    
    lvProcesses.Items.Add("item");

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    It's easier to have a data binding source associated with the list view, then set the data source to a bindingList that contains your element type, and the DataMember property set to the data the BindingList Holds. Then when you add your stuff to the bindinglist, it automatically updates the listview control.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    Unfortunatly that's not really an option here, I'm trying to populate the ListView with the list of processes currently running on a target system. So it has to be done at runtime.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What is lvProcesses?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    lvProcesses is just the ListView Control itself

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it seems to me you are trying to add string "item" instead of object ListViewItem item
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    I've had it both ways around, neither works. The string version is a valid version of the overloaded method, but the issue seems to be that the ListViewItemContainter Items (that's lvProcesses.Itemsin this case) hasn't been instanciated, however you can't individually instaciate it as its read only.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    1) You forgot to (manually) create a System.Windows.Forms.ListView object and assign it to lvProcesses
    2) You are trying to add the item before the call to InitializeComponent (in your form class)

    1) is if you create your controls manually, 2) is if you're using the visual designer
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    Talking

    Quote Originally Posted by Magos View Post
    1) You forgot to (manually) create a System.Windows.Forms.ListView object and assign it to lvProcesses
    2) You are trying to add the item before the call to InitializeComponent (in your form class)

    1) is if you create your controls manually, 2) is if you're using the visual designer
    Ahaha!! Fantastic thanks, I'd put it in the Initial method of the form to test things but before InitalizeComponents had been called as a test. That's solved it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Listview control
    By Mavix in forum C# Programming
    Replies: 6
    Last Post: 12-21-2007, 01:53 PM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. A few questions on ListView Control...
    By Devil Panther in forum Windows Programming
    Replies: 0
    Last Post: 09-05-2003, 02:33 PM
  4. Edit control on listview
    By knutso in forum Windows Programming
    Replies: 5
    Last Post: 08-10-2003, 09:11 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM