Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. WMI Exceptions/Updating List View

WMI Exceptions/Updating List View

Scheduled Pinned Locked Moved C#
csharptutorialquestionannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    autekre
    wrote on last edited by
    #1

    I've only just started with c# so these are probably stupid questions. 1. Why is it displaying certain information (for example CommandLine from Win32_Process) throws up a NullReferenceException, yet other information from the same collection works fine and how would I go about avoiding this (ie; actually getting the information rather than just handling the exception,) I'm displaying the information in a list view fwiw, using foreach (ManagementObject MO in processQueryCollection) { ListViewItem processItem = new ListViewItem(MO["Whatever"].ToString()); processItem.SubItems.Add(MO["Whatever"].ToString() + " Bytes"); ...... lstProcesses.Items.AddRange(new ListViewItem[] {processItem}) } You get the idea. 2. What would be the best way to update the list view on a timer event ? I've got the timer working but obviously if I call the method that contains the above code it replicates the data, adding new columns and rows rather than updating the existing data and if I call a clear method it ends up looking very strange. TIA.

    P 1 Reply Last reply
    0
    • A autekre

      I've only just started with c# so these are probably stupid questions. 1. Why is it displaying certain information (for example CommandLine from Win32_Process) throws up a NullReferenceException, yet other information from the same collection works fine and how would I go about avoiding this (ie; actually getting the information rather than just handling the exception,) I'm displaying the information in a list view fwiw, using foreach (ManagementObject MO in processQueryCollection) { ListViewItem processItem = new ListViewItem(MO["Whatever"].ToString()); processItem.SubItems.Add(MO["Whatever"].ToString() + " Bytes"); ...... lstProcesses.Items.AddRange(new ListViewItem[] {processItem}) } You get the idea. 2. What would be the best way to update the list view on a timer event ? I've got the timer working but obviously if I call the method that contains the above code it replicates the data, adding new columns and rows rather than updating the existing data and if I call a clear method it ends up looking very strange. TIA.

      P Offline
      P Offline
      Praveen Nayak
      wrote on last edited by
      #2

      1. This point is pretty simple, there could be many cases where you are trying to access a property of a null item which is throwing the exception. For example, if MO["Whatever"] doesn't return a valid object, it returns a null. You are then trying to access the ToString() method over this object, which is actually null, so it is like null.ToString(). You cannot access any of the properties on null. Ideally you must perform checks like object temp = MO["Whatever"] if (temp != null) { processItem.SubItems.Add(temp.ToString() + " Bytes"); } Note: Null checks must be a "taken for granted" coding style. 2. I am thinking, don't clear the list. You then use listView.Contains(..) method (or a similar implementation by you) to first check if the new item is present and if it is, don't add it. Add a reference to all the current items of the list view not checked to a collection. This will be the list that got removed (if you need that) and then remove these items from the list. There will be much less confusion this way. There has to be more to life than just this -- modified at 1:01 Friday 24th February, 2006

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups