Win32_Printer: System.NullReferenceException - Additional information: Object reference not set to a

J

J M

Getting NullReferenceException walking through printer properties:

ObjectQuery names = new ObjectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher printQueues = new
ManagementObjectSearcher(names);

ManagementObjectCollection queryCollection1 = printQueues.Get();

foreach( ManagementObject mo in queryCollection1 )
{
queueStatus = printerStatusMeaning(Convert.ToInt32(mo["PrinterStatus"]));

PropertyDataCollection Props = mo.Properties;
foreach (PropertyData prop in Props)
{
Console.WriteLine("=====>" + prop.Name.ToString() + " " +
prop.Value.ToString());
}
}

Also getting same error on following queries:

mo["Comment"].ToString()
mo["Location"].ToString()

Any assistance is appriciated! TIA!
 
W

Willy Denoyette [MVP]

That means that the property doesn't exist (they are empty - check your printer properties).
Now there are two things you can do:
- Wrap your property enum code in a try/catch block, and handle the NullRef exception (preferable).
- Make sure all properties are filled in.

Willy.
 
J

J M

Willy Denoyette said:
That means that the property doesn't exist (they are empty - check your printer properties).
Now there are two things you can do:
- Wrap your property enum code in a try/catch block, and handle the NullRef exception (preferable).
- Make sure all properties are filled in.

Willy.


J M said:
Getting NullReferenceException walking through printer properties:

ObjectQuery names = new ObjectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher printQueues = new
ManagementObjectSearcher(names);

ManagementObjectCollection queryCollection1 = printQueues.Get();

foreach( ManagementObject mo in queryCollection1 )
{
queueStatus = printerStatusMeaning(Convert.ToInt32(mo["PrinterStatus"]));

PropertyDataCollection Props = mo.Properties;
foreach (PropertyData prop in Props)
{
Console.WriteLine("=====>" + prop.Name.ToString() + " " +
prop.Value.ToString());
}
}

Also getting same error on following queries:

mo["Comment"].ToString()
mo["Location"].ToString()

Any assistance is appriciated! TIA!


I am not having any luck putting wrap around NullException. I used
try loop with NullReferenceException catch as provided on example but
no luck.

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top