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

  • Thread starter Thread starter J M
  • Start date Start date
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!
 
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.
 
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!
 
Back
Top