[SNMP4J] NullPointerException: snmp.send(pdu, target);

Kelvin Williams kwilliams at altuscgi.com
Mon Jan 11 22:39:21 CET 2010


First, please forgive my ignorance.  I'm no programmer, but have a need.
 I'm trying to query a host for a particular OID, and the code appears to be
fine until snmp.send(pdu, target) is called, at that point a
NullPointerException is thrown.  I know this is a valid OID via tests done
on using snmpget (net-snmp).  Can someone please tell me what I've done
wrong before I pull what little hair left out.

Thanks in advance.
kw

package net.altuscgi.tools;
import java.io.IOException;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class SNMP {
    public static String getSnmp () {
        String retVal;
        try {
             CommunityTarget target = new CommunityTarget();
             OctetString targetCommunity = new OctetString("letmesee");
             Address targetAddress = new UdpAddress("1.1.1.1/161");
             TransportMapping transport = new DefaultUdpTransportMapping();
             Snmp snmp = new Snmp(transport);
             target.setCommunity(targetCommunity);
             target.setAddress(targetAddress);
             target.setRetries(2);
             target.setTimeout(2000);
             target.setVersion(SnmpConstants.version2c);
             PDU pdu = new PDU();
             pdu.add(new VariableBinding(new OID(new int[]
{1,3,6,1,2,1,2,2,1,4,10})));
             pdu.setType(PDU.GET);

             ResponseEvent resp;

             Variable oRC;
             int respLength;

             try {
                 resp = snmp.send(pdu, target);
              } catch (NullPointerException e) {

             }

             } catch (IOException e) {
                 System.out.println("IOException: " + e.getMessage());

             }

    }
}


-- 
Kelvin Williams
Sr. Service Delivery Engineer


"If you only have a hammer, you tend to see every problem as a nail." --
Abraham Maslow



More information about the SNMP4J mailing list