0

I want to get the WMI values for some of the attributes mentioned in the local IList. I'm doing a WMI query and getting all the attribute values in PropertyData[] by getting the values out of queryObj.Proerties.

I'm preparing a Inner join Linq query as I'm interested in only the property name in my locally created list but it is throwing NullrefernceException. Dies the Linq query doesn't work on array or I need to initialize this PropertData object in Linq query before accessing this object property for comparision?

Please help me on this as I found this very weird and wasted lot of time on this. Following is the code snippet:

    IDictionary<string, IList<string>> biosWmiAttributes = new Dictionary<string, IList<string>>();
                    biosWmiAttributes["Win32_BIOS"] = new List<string>{"Availability",
                                                            "BatteryStatus",
                                                            "Description",
                                                            "DeviceID",
                                                            "EstimatedRunTime",
                                                            "Name",
                                                            "Status",
                                                            "PowerManagementSupported"};

     ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");
foreach (ManagementObject queryObj in searcher.Get())
{
                            bios = new Dictionary<string, string>();

                            StringBuilder biosCharacteristics = new StringBuilder();
                            PropertyData[] wmiAttributes = new PropertyData[100];
                            queryObj.Properties.CopyTo(wmiAttributes, 0);

var some = from biosAttribute in biosWmiAttributes["Win32_BIOS"]
                                   join wmiAttribute in wmiAttributes on biosAttribute equals wmiAttribute.Name into biosAttributes
                                   select new {Name=biosAttributes};
}
Bobby
  • 169
  • 1
  • 2
  • 9
  • Where does it throw the error? Did you check if the objects involved are non null/ – shree.pat18 Aug 05 '14 at 07:30
  • Have a look here: [Detecting what the target object is when NullReferenceException is thrown](http://stackoverflow.com/questions/115573/detecting-what-the-target-object-is-when-nullreferenceexception-is-thrown/116424#116424) – Matthijs Aug 05 '14 at 07:33
  • In the "wmiAttributes" array I see that entries of type PropertyData is present correctly and have values.But when I try to access any property of the element in this array in linq query then Obect Reference Not Set error is coming. So I want property values of string present in my local list to be fetched from this PropertyData[]. Can u please provide the linq query? – Bobby Aug 05 '14 at 07:35
  • I don't think it is duplicate of any null reference exception question already asked as the correct answer is garbage value present in the array which has to be dynamically initialized as mentioned in the answer of this question. All the issues will not fall under a common umbrella of "NullRefernceException" type. – Bobby Jan 14 '15 at 19:24

1 Answers1

2
PropertyData[] wmiAttributes = new PropertyData[100];
queryObj.Properties.CopyTo(wmiAttributes, 0);

Here is the problem, you're allocating 100 elements in array where actual size will not be 100. When Linq query tries to access your array you get the NullReferenceException. Change it to the following:

PropertyData[] wmiAttributes = queryObj.Properties
                                       .Cast<PropertyData>()
                                       .ToArray();

Also take a look at What is a NullReferenceException, and how do I fix it? to learn more.

Community
  • 1
  • 1
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • You are correct,becoz of this assignment only "NullrefernceException" was coming. Thanks Sriram,I'll mark this as an answer. – Bobby Aug 05 '14 at 08:42