I am using sharpsnmp-pro to parse MIB files.
From the examples, I have the following code:
var registry = new SimpleObjectRegistry();
var collector = new ErrorRegistry();
registry.Tree.Collector = collector;
registry.Import(Parser.Compile(GetLocation("SNMPv2-SMI.txt"), collector));
registry.Import(Parser.Compile(GetLocation("SNMPv2-CONF.txt"), collector));
registry.Import(Parser.Compile(GetLocation("SNMPv2-TC.txt"), collector));
registry.Import(Parser.Compile(GetLocation("SNMPv2-MIB.txt"), collector));
registry.Import(Parser.Compile(GetLocation("SNMPv2-TM.txt"), collector));
registry.Refresh();
// get an item with a DESCRIPTION; this works fine...
Definition item = registry.Tree.Find("SNMPv2-MIB", "sysDescr");
IEntity entity = item.DisplayEntity;
Console.WriteLine(entity.DescriptionFormatted());
// now find an item without a DESCRIPTION; this raises null reference exception
item = registry.Tree.Find("SNMPv2-MIB", "snmpTrap");
entity = item.DisplayEntity;
Console.WriteLine(entity.DescriptionFormatted());
The second call raises null reference exception
Object reference not set to an instance of an object.
at Lextm.SharpSnmpPro.Mib.ObjectIdentifierMacro.쬯()
at Lextm.SharpSnmpPro.Mib.EntityExtensions.DescriptionFormatted(IEntity entity)
at snmptranslate.Program.Main(String[] args)
How can you determine whether the the entity in question has a description prior to calling the description to avoid the null reference (preferably without capturing in a try..catch)?
For example, pulling out a new entity using linq to export into an excel spreadsheet, I was hoping DescriptionFormatted() would just return null string, but it fails.
var content = entities.Select(y => new { Module = y.Module.Name, y.Name, OID = y.GetObjectIdentifier(), Description = y.DescriptionFormatted() })