[SNMP4J] java.lang.NullPointerException: Context engine ID must not be null

FLORENT Philippe Philippe.FLORENT at edenred.com
Thu Aug 11 13:13:42 CEST 2016


Ok, I downgraded to v 1.1... and now the bug is gone

-----Original Message-----
From: Frank Fock [mailto:fock at agentpp.com] 
Sent: jeudi 11 août 2016 10:26
To: FLORENT Philippe
Cc: snmp4j at agentpp.org
Subject: Re: [SNMP4J] java.lang.NullPointerException: Context engine ID must not be null

Hi,
If walk (which is a GETNEXT in fact) and GET behave differently then most likely you are using the wrong OID (wrong instance identifier). Sometimes the agent is broken too.
In your case, you should put the Snmp instance in listen mode to receive answers from the agent. See FAQ for details.
Best regards 
Frank

> Am 11.08.2016 um 09:29 schrieb FLORENT Philippe <Philippe.FLORENT at edenred.com>:
> 
> Hello everyone,
> 
> 
> -          Doing a get on some OID  give me "unkown instance" although they work with snmpwalk on the command line and GET works with mib explorer
> 
> 
> 
> -          So I try to implement an snmp walk in snmp4j but I keep getting that error
> 
> Here is the code
> 
> I tried to use request.getContextEngineID(), but it is also null
> 
> public class SnmpProber
> {
>   private Snmp snmp;
>    private UserTarget target;
> 
>    private byte[] authEngineId0=null;
> 
>    public SnmpProber(String ipAddress,String port,String userName,String authPass,String privPass)
>    {
>        try
>        {
>            Address targetAddress = GenericAddress.parse("udp:"+ipAddress+"/"+port);
>            TransportMapping transport = new DefaultUdpTransportMapping();
>            snmp = new Snmp(transport);
>            USM usm = new USM(SecurityProtocols.getInstance(),new OctetString(MPv3.createLocalEngineID()), 0);
>            SecurityModels.getInstance().addSecurityModel(usm);
>            transport.listen();
> 
>            target = new UserTarget();
>            target.setAddress(targetAddress);
>            target.setRetries(1);
>            target.setTimeout(5000);
>            target.setVersion(SnmpConstants.version3);
>            target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
>            target.setSecurityName(new OctetString(userName));
> 
>            authEngineId0 = snmp.discoverAuthoritativeEngineID(target.getAddress(), 5000);
> 
>            if(authEngineId0==null)
>                System.out.println("cannot connect");
>            else
>            {
>                // add user to the USM
>                UsmUser usmUser=new UsmUser(new OctetString(userName),
>                                      AuthSHA.ID,
>                                      new OctetString(authPass),
>                                      PrivAES128.ID,
>                                      new OctetString(privPass));
> 
>                snmp.getUSM().addUser(new OctetString("v3DefaultUser"),
>                                      new OctetString(authEngineId0),
>                                      usmUser);
>            }
>        } catch (IOException ex) {
>            Logger.getLogger(SnmpProber.class.getName()).log(Level.SEVERE, null, ex);
>        }
>    }
> 
>    public boolean isOk()
>    {
>        return authEngineId0!=null;
>    }
> 
>    class WalkCounts {
>        public int requests;
>        public int objects;
>    }
> 
>    public List<VariableBinding> walk(String oid)
>    {
>        final List<VariableBinding> result;
>        result = new LinkedList<VariableBinding>();
>        ScopedPDU request;
>        request = new ScopedPDU();
>        request.setType(PDU.GETNEXT);
> 
>        request.add(new VariableBinding(new OID(oid)));
> 
>        request.setNonRepeaters(0);
> 
>        OID rootOID = request.get(0).getOid();
> 
>        final WalkCounts counts = new WalkCounts();
>        final long startTime = System.currentTimeMillis();
>        TreeUtils treeUtils = new TreeUtils(snmp, new DefaultPDUFactory());
> 
>        TreeListener treeListener = new TreeListener()
>        {
>            public boolean next(TreeEvent e) {
>                counts.requests++;
> 
>                if (e.getVariableBindings() != null) {
>                    VariableBinding[] vbs = e.getVariableBindings();
>                    counts.objects += vbs.length;
>                    for (int i = 0; i < vbs.length; i++) {
>                        if (result != null) {
>                            result.add(vbs[i]);
>                        }
>                        System.out.println(vbs[i].toString());
>                    }
>                }
>                return true;
>            }
> 
>            @Override
>            public void finished(TreeEvent e) {
>                if ((e.getVariableBindings() != null)
>                        && (e.getVariableBindings().length > 0)) {
>                    next(e);
>                }
> 
>                System.out.println("Total requests sent:    "+ counts.requests);
>                System.out.println("Total objects received: "+ counts.objects);
>                System.out.println("Total walk time:        "+ (System.currentTimeMillis() - startTime)+ " milliseconds");
> 
>                if (e.isError()) {
>                    System.err.println("The following error occurred during walk:");
>                    System.err.println(e.getErrorMessage());
>                }
>                synchronized (this) {
>                    this.notify();
>                }
>            }
> 
>            @Override
>            public boolean isFinished() {
>                return true;
>            }
>        };
> 
>        synchronized (treeListener)
>        {
>            treeUtils.getSubtree(target, rootOID, null, treeListener);  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< exception here
>            try
>            {
>                treeListener.wait();
>            }
>            catch (InterruptedException ex)
>            {
>                System.out.println("Tree retrieval interrupted: "+ex);
>                Thread.currentThread().interrupt();
>            }
>        }
> 
>        return result;
>    }
> 
>    public void Release()
>    {
>        try {
>            snmp.close();
>        } catch (IOException ex) {
>            Logger.getLogger(SnmpProber.class.getName()).log(Level.SEVERE, null, ex);
>        }
>    }
> }
> 
> 
> 
> 
> _______________________________________________
> SNMP4J mailing list
> SNMP4J at agentpp.org
> https://oosnmp.net/mailman/listinfo/snmp4j




More information about the SNMP4J mailing list