Specifying context engine id using TableUtils

I am using my SNMPv3 agent to retrieve table entries from an SNMPv3 agent.
Below is my sample code:

public List<RouteEntry> getRoutingTable(OID oid) {
    TableUtils tableUtils = new TableUtils(snmp, new DefaultPDUFactory());
    List<TableEvent> events = tableUtils.getTable(getTarget(), new OID[] { oid }, null, null);
    List<RouteEntry> routingTable = new ArrayList<RouteEntry>();
    for (TableEvent event : events) {
    	RouteEntry app = new RouteEntry();
        VariableBinding[] varBind = event.getColumns();
        if (varBind != null) {
            app.setNetId(varBind[0].getVariable().toInt());
            app.setDestId(varBind[1].getVariable().toInt());
            app.setNextHopId(varBind[2].getVariable().toInt());
            app.setCost(varBind[3].getVariable().toInt());
            app.setIp(varBind[4].getVariable().toString());
            app.setLinkQuality(varBind[5].getVariable().toInt());
            routingTable.add(app);
        }
    }
    return routingTable;
}

Unlike my get operation, I can specify the context engine id in:

     ScopedPDU pdu = new ScopedPDU();
     pdu.setContextName(new OctetString("public"));

I am receiving a “Context ID must not be null” error when tableUtils.getTable() is invoked. ie. see Logs below:

Exception in thread "main" java.lang.NullPointerException: Context engine ID must not be null
at org.snmp4j.ScopedPDU.setContextEngineID(ScopedPDU.java:69)
at org.snmp4j.util.DefaultPDUFactory.applyContextInfoToScopedPDU(DefaultPDUFactory.java:118)
at org.snmp4j.util.DefaultPDUFactory.createPDU(DefaultPDUFactory.java:103)
at org.snmp4j.util.TableUtils$TableRequest.sendNextChunk(TableUtils.java:465)
at org.snmp4j.util.TableUtils.getTable(TableUtils.java:120)
at snmp.SNMPManager.getRoutingTable(SNMPManager.java:235)
at logic.MappingController.getRoutingTable(MappingController.java:203)
at logic.MappingController.init(MappingController.java:182)
at rce.RCEController.init(RCEController.java:34)
at RCEEngine.main(RCEEngine.java:21)

Apparently you are not using the latest version, otherwise this exception would have not occurred.
To fix your code, set the contextEngineID on your DefaultPDUFactory to

defaultPDUFactory.setContextEngineID(new OctetString());
1 Like

Thanks :slight_smile:

I’m using snmp4j-2.8.3.jar