简体   繁体   中英

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

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);
           }
       }
   }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM