[SNMP4J] Strange NullPointerException...
Frank Fock
fock at agentpp.com
Fri May 12 22:36:00 CEST 2006
Hi,
The NullPointerException seems to be caused by a null
destAddress. Have you checked that it is set/created
correctly?
Best regards,
Frank
Menard, Matt wrote:
> First off let me say that I'm rather new to SNMP and this is the first
> time I've been involved in writing any SNMP code. The problem I'm
> having is when I try to send a GETBULK request to the SNMP agent (SET
> and TRAPS do not have this problem; the requests are sent to the server
> just fine). As soon as the "response = snmp.send(request, target);"
> line is executed, I receive the following exception.
>
>
>
> java.lang.NullPointerException
>
> at
> org.snmp4j.MessageDispatcherImpl.getTransport(MessageDispatcherImpl.java
> :204)
>
> at
> org.snmp4j.MessageDispatcherImpl.sendPdu(MessageDispatcherImpl.java:373)
>
> at org.snmp4j.Snmp.sendMessage(Snmp.java:955)
>
> at org.snmp4j.Snmp.send(Snmp.java:802)
>
> at org.snmp4j.Snmp.send(Snmp.java:758)
>
>
>
> After trying, in vain, to figure this problem out, I thought that if I
> passed the Transport to the snmp.send() method directly, it would solve
> the problem. That turned out to be incorrect and it just caused all the
> requests to time out. Here are some of the particulars about this
> project:
>
>
>
> SNMP version: 3
>
> SNMP authentication: MD5
>
> SNMP privacy: AES-128
>
> IDE: JBuilder 2005
>
> Java version: 1.4.1_09
>
> SNMP4J version: 1.7.1
>
>
>
> I've been banging my head against the wall trying to figure this out for
> a few days now. Like I said, this only happens when I try to execute a
> GETBULK request. I even went so far as to go through the
> SnmpRequest.java file that comes with the SNMP4J bundle and copy the
> code out there into my class (which you can see in the code included
> below) just incase there was something subtle I was missing.
>
>
>
> Any help would be greatly appreciated!
>
>
>
> Thanks,
> Matt
>
>
>
> //------------- CODE --------------------------------
>
> private PDU sendRequest(String ipAddress,
>
> int port,
>
> String username,
>
> String authPassphrase,
>
> String privPassphrase,
>
> PDU request,
>
> CommandResponder responder)
>
> {
>
> PDU responsePDU = null;
>
>
>
> try
>
> {
>
> Snmp snmp = createSnmpSession(username, authPassphrase,
> privPassphrase);
>
>
>
>
>
>
>
> // Create the target and set the individual attributes of this SNMP
>
> // transaciton.
>
> Target target = createTarget(username,
>
> authPassphrase,
>
> privPassphrase,
>
> GenericAddress.parse("udp:" + ipAddress
> + "/" + port));
>
>
>
> if(responder != null)
>
> {
>
> snmp.addCommandResponder(responder);
>
> }
>
>
>
> snmp.listen();
>
>
>
> ResponseEvent response = null;
>
>
>
> // This is the line that generates the exception.
>
> response = snmp.send(request, target);
>
>
>
> responsePDU = response.getResponse();
>
>
>
> snmp.close();
>
> }
>
>
>
> catch(IOException ioe)
>
> {
>
> ioe.printStackTrace();
>
> }
>
>
>
> catch(Exception e)
>
> {
>
> e.printStackTrace();
>
> }
>
>
>
> return responsePDU;
>
> }
>
>
>
> //-------------------------------------------
>
> // addUsmUser, lifted from the SnmpRequest.java class contained
>
> // in the SNMP4J package.
>
> private void addUsmUser(Snmp snmp,
>
> String username,
>
> String authPassphrase,
>
> String privPassphrase)
>
> {
>
> snmp.getUSM().addUser(new OctetString(username),
>
> new UsmUser(new OctetString(username),
>
> AuthMD5.ID,
>
> new OctetString(authPassphrase),
>
> PrivAES128.ID,
>
> new OctetString(privPassphrase)));
>
> }
>
>
>
> //-------------------------------------------
>
> // Portions of this code were derived from the SnmpRequest.java class...
>
> private Snmp createSnmpSession(String username,
>
> String authPassphrase,
>
> String privPassphrase) throws IOException
>
> {
>
> // Setup a transport over UDP
>
> AbstractTransportMapping transport = new
> DefaultUdpTransportMapping();
>
>
>
> Snmp snmp = new Snmp(transport);
>
>
>
> // Setup the user security model which will provide privacy
> (encryption)
>
> // and authentication.
>
> USM usm = new USM(SecurityProtocols.getInstance(),
>
> new OctetString(MPv3.createLocalEngineID()), 0);
>
>
>
> SecurityModels.getInstance().addSecurityModel(usm);
>
> addUsmUser(snmp, username, authPassphrase, privPassphrase);
>
> return snmp;
>
> }
>
>
>
> //-------------------------------------------
>
> // This class was also derived from the SnmpRequest.java
>
> // class.
>
> private Target createTarget(String username,
>
> String authPassphrase,
>
> String privPassphrase,
>
> Address targetAddress)
>
> {
>
> UserTarget target = new UserTarget();
>
>
>
> if(authPassphrase != null)
>
> {
>
> if(privPassphrase != null)
>
> {
>
> target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
>
> }
>
>
>
> else
>
> {
>
> target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);
>
> }
>
> }
>
>
>
> else
>
> {
>
> target.setSecurityLevel(SecurityLevel.NOAUTH_NOPRIV);
>
> }
>
>
>
> target.setAddress(targetAddress);
>
> target.setRetries(1);
>
> target.setTimeout(5000);
>
> target.setVersion(SnmpConstants.version3);
>
> target.setSecurityName(new OctetString(username));
>
>
>
> return target;
>
> }
>
> _______________________________________________
> SNMP4J mailing list
> SNMP4J at agentpp.org
> http://lists.agentpp.org/mailman/listinfo/snmp4j
--
AGENT++
http://www.agentpp.com
http://www.mibexplorer.com
http://www.mibdesigner.com
More information about the SNMP4J
mailing list