9 Replies - 1475 Views - Last Post: 08 June 2012 - 11:36 AM Rate Topic: ***-- 2 Votes

#1 justanub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-May 12

NullReferenceException Error

Posted 24 May 2012 - 10:48 AM

Hey guys,

I have this Windows Form Application that has a listview of services in Win_32. In my application I am trying to implement a refresh button.

However while debugging I keep getting this NullReferenceException error. :basecase:

I did notice that when I hovered over this part of my code
foreach (ManagementObject moService in mcServices.GetInstances())



I get a notification that says this "ManagementObjectCollection ManagementClass.GetInstances()(+3 overload(s))"

Here is the my full code


public void loadListBox()
        {
            foreach (ListViewItem svc in listView1.Items)
            {
                listView1.Items.Remove(svc);
            }

            ManagementClass mcServices = new ManagementClass("Win32_Service");
            foreach (ManagementObject moService in mcServices.GetInstances())
            {
                ListViewItem newItem = new ListViewItem(moService.GetPropertyValue("Caption").ToString());
                newItem.SubItems.Add(moService.GetPropertyValue("Description").ToString());
                if (moService.GetPropertyValue("State").ToString().Equals("Running"))
                {
                    newItem.SubItems.Add(moService.GetPropertyValue("State").ToString());
                }
                else
                {
                    newItem.SubItems.Add("");
                }
                newItem.SubItems.Add(moService.GetPropertyValue("StartMode").ToString());
                newItem.SubItems.Add(moService.GetPropertyValue("StartName").ToString());
                listView1.Items.Add(newItem);
            }
        }

private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            loadListBox();
        }



The exception occurs here


     newItem.SubItems.Add(moService.GetPropertyValue("Description").ToString());




Please help :surrender:

Is This A Good Question/Topic? 0
  • +

Replies To: NullReferenceException Error

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: NullReferenceException Error

Posted 24 May 2012 - 11:29 AM

Covered in this article:
What does this error mean?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: NullReferenceException Error

Posted 24 May 2012 - 11:35 AM

View Postjustanub, on 24 May 2012 - 11:48 AM, said:

The exception occurs here

     newItem.SubItems.Add(moService.GetPropertyValue("Description").ToString());




put a breakpoint there. There are a limtied number of items there that could be null:
newItem
newItem.SubItems
the returned result of moService.GetPropertyValue("Description")

I'd break down the complex line into several lines so you can see which it is.
You also have to stop programming as if nothing can go wrong. You have to assume everything will be wrong and will break: Then program defensively for that situation.

var PropVal = moService.GetPropertyValue("Description");// see if this is null
if (newItem != null && newItem.SubItems != null) newItem.SubItems.Add(PropVal.ToString());

Was This Post Helpful? 1
  • +
  • -

#4 justanub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-May 12

Re: NullReferenceException Error

Posted 24 May 2012 - 11:48 AM

I still get the same error, could there possibly be null ManagementObjects in mcServices?
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: NullReferenceException Error

Posted 24 May 2012 - 02:13 PM

You get it *where*?
Which item is null?
Was This Post Helpful? 0
  • +
  • -

#6 justanub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-May 12

Re: NullReferenceException Error

Posted 07 June 2012 - 12:50 PM

I get this error "NullReferenceException was unhandled - Object reference not set to an instance of an object."

Here

if (newItem != null && newItem.SubItems != null) newItem.SubItems.Add(PropVal.ToString());



Specifically Visual Studio highlights this part of the line

newItem.SubItems.Add(PropVal.ToString());



And offers these troubleshooting tips:

  • Use the "new" keyword to create an object instance.
  • Check to determine if the object is null before calling the method


Not exactly sure what to do here since you already recommended that I check to see if any of the objects in my list are null.

Any ideas?
Was This Post Helpful? 0
  • +
  • -

#7 justanub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-May 12

Re: NullReferenceException Error

Posted 07 June 2012 - 12:56 PM

In case it helps here is a more wholistic view of code

private void loadListBox()
        {
            foreach (ListViewItem svc in listView1.Items)
            {
                listView1.Items.Remove(svc);
            }

            ManagementClass mcServices = new ManagementClass("Win32_Service");
            foreach (ManagementObject moService in mcServices.GetInstances())
            {
                ListViewItem newItem = new ListViewItem(moService.GetPropertyValue("Caption").ToString());
                var PropVal = moService.GetPropertyValue("Description");// see if this is null
                if (newItem != null && newItem.SubItems != null) newItem.SubItems.Add(PropVal.ToString());
                if (moService.GetPropertyValue("State").ToString().Equals("Running"))
                {
                    newItem.SubItems.Add(moService.GetPropertyValue("State").ToString());
                }
                else
                {
                    newItem.SubItems.Add("");
                }
                newItem.SubItems.Add(moService.GetPropertyValue("StartMode").ToString());
                newItem.SubItems.Add(moService.GetPropertyValue("StartName").ToString());
                listView1.Items.Add(newItem);
            }
        }


Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: NullReferenceException Error

Posted 07 June 2012 - 01:06 PM

View Postjustanub, on 07 June 2012 - 01:50 PM, said:

Here

if (newItem != null && newItem.SubItems != null) newItem.SubItems.Add(PropVal.ToString());



Specifically Visual Studio highlights this part of the line

newItem.SubItems.Add(PropVal.ToString());




And I ask AGAIN: Which object is NULL? There are only 3 you have to check
  • newItem
  • newItem.SubItems
  • PropVal


Did you look at the tutorial I linked to you before so you could learn how to determine this?

This post has been edited by tlhIn`toq: 07 June 2012 - 01:08 PM

Was This Post Helpful? 1
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: NullReferenceException Error

Posted 07 June 2012 - 01:12 PM

Let's look at this another way...


13 if (newItem != null && newItem.SubItems != null) newItem.SubItems.Add(PropVal.ToString());

You know that newItem isn't null because your if statement checks for that.
You know that newItem.SubItems isn't null because your if statement checks for that.

You get your PropVal on line 12. Do you ever check if it null before trying to use it?

Put a breakpoint on line 13 and open the locals pallet so you can see the values of these three things. Then take a screen capture and show us what you see.
Was This Post Helpful? 1
  • +
  • -

#10 justanub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-May 12

Re: NullReferenceException Error

Posted 08 June 2012 - 11:36 AM

Ahhhhh I never checked to see if PropVal was null. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1