Anonymous22234587655479

Good Evening.

I have been working on a project to monitor certain aspects of processes and have encountered some difficulties. While executing the code below, I get the following exception:

System.NullReferenceException: Object reference not set to an instance of an object.


ObjectQuery query = new ObjectQuery("SELECT Name,CreationDate,ExecutablePath FROM Win32_Process");
ManagementObjectSearcher processes = new ManagementObjectSearcher(this.scope, query);
ManagementObjectCollection returnedProcesses = processes.Get();

foreach (ManagementObject process in returnedProcesses)
{ //handle process }


The first line inside the for loop is not executed which leads me to believe that it is actually the foreach statement is causing the problem.

After googling for a bit I have been unable to find any reason for the NullReference.

Thanks in advance for any suggestions.

Ben




Re: .NET Base Class Library ManagementObject - NullReference Exception

ahmedilyas

perhaps the object it is iterating through is actually null and you are trying to perform some operation with the null object causing a NullRefException.

WMI does bring back null results also in a collection.

what happens if you add an if statement to see if the current object is null or not - if it is not, continue on with what you need to do and dont perform operations on the null object






Re: .NET Base Class Library ManagementObject - NullReference Exception

Anonymous22234587655479

I have given that some thought. If I check returnedProcesses.Count I always get a non-zero value which would seem to indicate that there are processes in returnedProcesses. I also would like to think that, since there are processes running, that returnedProcesses would countain a list of those processes. However, I appreciate your suggestion and will implement it when I get home from work.




Re: .NET Base Class Library ManagementObject - NullReference Exception

Feng Chen - MSFT

Hi Anonymous22234587655479,

Since you have came across an exception, Could you please cover the exception in more details You can post its stack trace, and inner exception here.

And we¡¯d appreciate if you can cover ¡°this.scope¡± in more details.

Thanks!






Re: .NET Base Class Library ManagementObject - NullReference Exception

Anonymous22234587655479

Here is the code that I am trying to run. I don't even get into the foreach loop to do all that magic that I am hoping to, it seems to throw the exception at the loop call.

try
{
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Process");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(this.scope, query);
ManagementObjectCollection collection = searcher.Get();
this.log.WriteEvent(new EventInstance(1, 1), "App Count " + collection.Count.ToString()); //debug

foreach (ManagementObject process in collection)
{
//do some magical things here
}


this.scope: ManagementScope scope = new ManagementScope("\\\\" + Environment.MachineName + "\\root\\CIMV2")

Inner exception doesn't seem to give me any information. I am using execption.InnerException.ToString() and writing it to the console and nothing is there.

The exception that I am getting is this:
System.NullReferenceException: Object reference not set to an instance of an object.


Here is the result of my stack trace:

at ApplicationMonitor.ApplicationMonitor.ApplicationEventArrived(Object sender, EventArrivedEventArgs e)
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Management.WmiDelegateInvoker.FireEventToDelegates(MulticastDelegate md, ManagementEventArgs args)
at System.Management.ManagementEventWatcher.FireEventArrived(EventArrivedEventArgs args)
at System.Management.SinkForEventQuery.Indicate(IntPtr pWbemClassObject)


Thanks for any suggestions.
Ben



*** Update ***
After a friend of mine looked at the code, we made a slight modification to make sure I wasn't actually getting into the loop. As it turns out I am getting into the loop, which is good and bad. Good because I can stop banging my head against the wall trying to figure out what is wrong with the code outside the loop and bad because I have to see about fixing the new problem. I'll post again if I get stuck with the code inside the loop.






Re: .NET Base Class Library ManagementObject - NullReference Exception

Feng Chen - MSFT

Hi Anonymous22234587655479,

Based on the stack trace, the exception is thrown from within the foreach loop. So could you please show more source code of ¡°//do some magical things here¡±

Thanks!