[SNMP4J] Null Pointer Exception in DefaultTcpTransportMapping.close()

Prusi, Jesse R jesse.r.prusi at lmco.com
Wed Aug 16 04:01:04 CEST 2006


Upon further investigation this fix did not solve the problem.  The
sockets are still not getting closed.  Any ideas?

Thanks,
Jesse

-----Original Message-----
From: snmp4j-bounces at agentpp.org [mailto:snmp4j-bounces at agentpp.org] On
Behalf Of Prusi, Jesse R
Sent: Tuesday, August 15, 2006 7:27 AM
To: snmp4j at agentpp.org
Subject: RE: [SNMP4J] Null Pointer Exception in
DefaultTcpTransportMapping.close()

I updated the close() method to catch the NPE when join() is called and
the server thread has already been set to null because run() exited.
This allows the sockets to be closed so the ports are freed up and it
seems to fix the problem.  Anyone else have a better solution?


  /**
   * Closes all open sockets and stops the internal server thread that
   * processes messages.
   */
  public synchronized void close() {
    if (server != null) {
      server.close();
      try {
        server.join();
      }
      catch (NullPointerException npe) {
        logger.warn(npe);
      }
      catch (InterruptedException ex) {
        logger.warn(ex);
      }
      server = null;
      for (Iterator it = sockets.values().iterator(); it.hasNext(); ) {
        SocketEntry entry = (SocketEntry)it.next();
        try {
          synchronized (entry) {
             entry.getSocket().close();
          }
          logger.debug("Socket to "+entry.getPeerAddress()+" closed");
        }
        catch (IOException iox) {
          // ingore
          logger.debug(iox);
        }
      }
      if (socketCleaner != null) {
        socketCleaner.cancel();
      }
      socketCleaner = null;
    }
  }

Thanks,
Jesse



-----Original Message-----
From: snmp4j-bounces at agentpp.org [mailto:snmp4j-bounces at agentpp.org] On
Behalf Of Prusi, Jesse R
Sent: Monday, August 14, 2006 6:44 PM
To: snmp4j at agentpp.org
Subject: [SNMP4J] Null Pointer Exception in
DefaultTcpTransportMapping.close()

Hello,

I'm using SNMP4J 1.7.3 on a Windows XP machine.

I find a get a Null Pointer Exception when snmp.close().  Here is my
sample code:

public class SnmpTest {

	public static void main(String[] args) {
		try{
			DefaultTcpTransportMapping myTransport = new
DefaultTcpTransportMapping(
					new
TcpAddress(InetAddress.getLocalHost(),
							10163));
			myTransport.setMaxInboundMessageSize(1400);
			myTransport.setConnectionTimeout(1000);
			
			Snmp snmp = new Snmp(myTransport);
			OctetString localEngineId = new
OctetString(MPv3.createLocalEngineID());
			USM usm = new
USM(SecurityProtocols.getInstance(), localEngineId, 0);
	
SecurityModels.getInstance().addSecurityModel(usm);
			snmp.listen();
			System.out.println("Listening on port 10163");
			for(int i=0; i < 10000; i++){
				System.out.println("spinning " + i);
			}
			snmp.close();
			System.out.println("Closed Snmp Session");
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
}

I get the following stack trace:

java.lang.NullPointerException
	at
org.snmp4j.transport.DefaultTcpTransportMapping.close(DefaultTcpTranspor
tMapping.java:186)
	at org.snmp4j.Snmp.close(Snmp.java:429)
	at SnmpTest.main(SnmpTest.java:31)

The NPE does not occur if I comment out the spinning for loop.  I
thought this might have something to do with the timeout setting of the
TCP transport mapping, but I still get the error after increasing the
timeout to 10000.  Any ideas why this is occurring?  Am I using the TCP
transport mapping incorrectly?

Thanks,  

Jesse Prusi

_______________________________________________
SNMP4J mailing list
SNMP4J at agentpp.org
http://lists.agentpp.org/mailman/listinfo/snmp4j


_______________________________________________
SNMP4J mailing list
SNMP4J at agentpp.org
http://lists.agentpp.org/mailman/listinfo/snmp4j





More information about the SNMP4J mailing list