0

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() })

enter image description here

Craig D
  • 487
  • 1
  • 5
  • 17
  • Console.WriteLine(entity.DescriptionFormatted()) in the second scenario – Craig D Jul 16 '18 at 05:14
  • Please see updated screen capture. Note that "Description" property also throws an exception. As you can see, item.DisplayEntity is definitely not null. – Craig D Jul 16 '18 at 05:33
  • There isn't much we can do then. You'll need to contact the author of `DescriptionFormatted` and raise the bug with them. – mjwills Jul 16 '18 at 05:41
  • To be clear, the duplicate is applicable, but I understand that it must be frustrating for you since you are unable to see the source code in question and thus fix it. One option you may consider doing is decompiling the DLL where the bug is, and sharing that in your question. – mjwills Jul 16 '18 at 06:54
  • but it isn't a duplicate. My question was asking for anyone who has experience with the library how to avoid getting errors on the interfaces. – Craig D Jul 16 '18 at 06:55
  • Thanks. I have asked to reopen the question. I have now got to the bottom on my issue. item.DisplayEntity is type IEntity, and in the exception case, it is type ObjectIdentifierMacro which does not implement description. If I am deemed worthy to have the question reopened I will document what I found and how to work around this limitation as per my original question. – Craig D Jul 17 '18 at 22:45
  • Vendor is investigating update to return null instead of an exception being raised. – Craig D Jul 18 '18 at 00:09

0 Answers0