[SNMP4J] snmp.close() sometimes throws npe
Thanos Panousis
pthanos at gmail.com
Mon Sep 7 09:51:41 CEST 2009
Hello all,
I would like to ask for some advice on why I keep getting a NPE when
trying to close the snmp instance. In the code bellow, just after
sending the request and getting back a result, I try to call
snmp.close(). This sometimes throws the npe. I have "protected" it
with a check for null, but it does not seem to work(it happens after
the not null check?).
Since snmp is never closed before, I don't see why snmp.close() should
throw an npe. This code is run from within a thread that does this
task and shares nothing with other threads. I have noticed that this
npe happens more often when the number of threads is higher.
protected String getValueFromBase(Sector sector) throws IOException{
CommunityTarget tar = new CommunityTarget();//setting up tar
Address addr = GenericAddress.parse("udp:" +sector.getIpAddress()
+ "/" + sector.getSnmpPort());
tar.setCommunity(new
OctetString(sector.getSnmpCommunity()));//Main.conf.getProperty("community",
"public")));
tar.setAddress(addr);
tar.setRetries(Integer.parseInt(Main.conf.getProperty("retry", "4")));
tar.setTimeout(Long.parseLong(Main.conf.getProperty("timeout", "400")));
tar.setVersion(SnmpConstants.version1);
TransportMapping trans = new DefaultUdpTransportMapping();
//if these fail no fetches will happen
trans.listen();
Snmp snmp = new Snmp(trans);
PDU tempPdu = new PDU();//creating PDU
tempPdu.add(new VariableBinding(new OID(oid)));
tempPdu.setType(PDU.GETNEXT);
ResponseEvent response = snmp.send(tempPdu, tar);
PDU responsePdu = response.getResponse();
//sometimes the snmp instance throws nullPointer!why?
if (snmp != null){
snmp.close();
trans.close();
}
//now parse the responsePdu and return result
}
The above code works but sometimes gives this kinf of errors
(truncated for clarity):
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:205)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
at org.snmp4j.Snmp.close(Unknown Source)
at wimossCollector.oidType.SectorOid.getValueFromBase(SectorOid.java:68)
Thank you.
Thanos Panousis.
More information about the SNMP4J
mailing list