Unknown Source Exception when using addProtocolCommandListener with library org.apache.commons.net.telnet.TelnetClient

436 views Asked by At

I'm using the following library...

org.apache.commons.net.telnet.TelnetClient;

I have this piece of code which I am using to connect to a server.

import org.apache.commons.net.*;
import org.apache.commons.net.ProtocolCommandListener
import org.apache.commons.net.ftp.FTPSClient
import org.apache.commons.net.telnet.TelnetClient;


String server = "some server";
String result = '';

//Create the telnet client and connect on port 43
TelnetClient telnetClient = new TelnetClient();
telnetClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
telnetClient.connect(server, 43);

When i get to the line with....

telnetClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

I get the following exception....

Caught: java.lang.NullPointerException Disconnected from the target VM, address: '127.0.0.1:3140', transport: 'socket' java.lang.NullPointerException at org.apache.commons.net.SocketClient.addProtocolCommandListener(SocketClient.java:772) at org.apache.commons.net.SocketClient$addProtocolCommandListener.call(Unknown Source)

Anybody know why I might be getting this exception? If I remove the line I don't so the exception is this line. And if i use the FTPS client i do not have this problem with SFTP from the same library!

Hi, thanks for responding, here is a simple example of some supported clients in the library, i'm guessing maybe that whois and ftp client do not support the listner !?

   import org.apache.commons.net.*;
   import org.apache.commons.net.ProtocolCommandListener;
   import org.apache.commons.net.ftp.FTPClient;
   import org.apache.commons.net.telnet.TelnetClient;
   import org.apache.commons.net.whois.WhoisClient;
   import java.io.PrintWriter;

   public class WhoisExample
   {
       public static void main(String args[])
       {
           try {
               WhoisClient whoisClient = new WhoisClient();
               whoisClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
           }
           catch(Exception e) {
               System.out.println("whois client exception" + e);
           }

           try {
               TelnetClient telnetClient = new TelnetClient();
               telnetClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
           }
           catch(Exception e) {
               System.out.println("telnet client exception" + e);
           }

           try {
               FTPClient ftpClient = new FTPClient();
               ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

               System.out.println("no ftp client exception here !");
           }
           catch(Exception e)
           {
               System.out.println("ftp client exception" + e);
           }
       }
   }
1

There are 1 answers

0
user6461581 On

I logged a bug with Apache commons net and it has been resolved as not a problem.

Basically .....'Some clients don't currently support the ProtocolCommand Listener.'

You can find out more from the following link....

https://issues.apache.org/jira/browse/NET-608