-1

So I'm trying to create an smtp from scratch, (assignment) and I'm trying to connect classes together but miserably failing and trying everything out. The compiler doesn't throw up any errors but when I run it I don't get very far. I tried calling the other classes with the .start thread but still, failure

If you could help me or give tips, I would really appreciate it

//Problem seems to be here: I have no idea how to correct it

   socketManager soketManager = null;
   DataInputStream clientDataIn = new   DataInputStream(soketManager.getInputStream());
    socketManager clientReaderSocket = soketManager;```
    public class socketManager {
    public Socket soc = null;`

`

//socketManager.java
public DataInputStream input = null;
public DataOutputStream output = null;

public socketManager(Socket socket) throws IOException {
    soc = socket;
    input = new DataInputStream(soc.getInputStream());
    output = new DataOutputStream(soc.getOutputStream());
}

public InputStream getInputStream() throws IOException {
    input = new DataInputStream(soc.getInputStream());
    return null;
}

public OutputStream getOutputStream() throws IOException {
    output = new DataOutputStream(soc.getOutputStream());
    return null;
}

}

Exception in thread "Thread-1" java.lang.NullPointerException at me.censored.loopback.SMTPclient.Client$ClientSocketManager.run(Client.java:117) at java.base/java.lang.Thread.run(Thread.java:834)

 // Main Method:- called when running the class file.
 public static void main(String[] args) throws UnknownHostException, IOException {
Port Declaration & Checks.
     String serverIP = "loopback";
     int defaultServerPort = 25;

     PortManager portManage = new PortManager();
     Thread portManagerThread = new Thread(portManage);
     portManagerThread.start();

 }// End of main

 static public class PortManager implements Runnable {
     Scanner userInput = new Scanner(System.in);

     int serverPort = 25;
     // Will accept only tcp/udp ports as of (2019) After several attempts the port
     // will be auto selected to default 25,
     // Should we accept parsed HEX?
     boolean portCompletion = false;
     short portTriesCounter = 1;

     public void run() {
         try {
             do {
                 // Asks user for server's port at the start up of the client.
                 System.out.println("Please enter the port the server is on.");
                 String userEntry = userInput.nextLine();
                 userInput.close();
                 try {
                     serverPort = Integer.parseInt(userEntry);
                     if (portTriesCounter != 5) { // Partial tries timeout.
                         if (serverPort != 0) { // "Port Zero" does not officially exist. It is defined as an invalid
                                                 // port
                                                 // number. But valid Internet packets can be formed and sent "over
                                                 // the
                                                 // wire"
                                                 // to and from "port 0" just as with any other ports.
                             if ((serverPort > 0 && serverPort <= 1023)
                                     || (serverPort >= 1024 && serverPort <= 49151)
                                     || (serverPort >= 49152 && serverPort <= 65535)) // Check for ports inside the
                                                                                         // tcp/udp
                                                                                         // range
                                 portCompletion = true;
                             else {
                                 System.out.println(
                                         "Wrong input! Make sure you are using correct numbers and port range! ");
                                 portCompletion = false;
                                 portTriesCounter++;
                             }
                             // End Check for ports inside the tcp/udp range
                         } else {
                             System.out.print("Wrong input! ");
                             portCompletion = false;
                             portTriesCounter++;
                         }
                         // End Check for zero
                     }
                     // End Too many attempts
                     else {
                         portTriesCounter = 5;
                         portCompletion = true;
                         System.out.print("Many wrong attemps. Selecting and trying the default port (25)... ");
                         try {
                             System.out.print("Success");
                             portCompletion = true;
                             serverPort = 25; // For SSL connections use port 465.

                         } catch (Exception except) {
                             portCompletion = false;
                         }
                     }
                 } catch (Exception except) {
                     portCompletion = false;
                     portTriesCounter++;
                 } finally {
                     userInput.close();
                 }
             } while (!portCompletion);
             ClientSocketManager clientSocketManage = new ClientSocketManager();
              Thread clientSocketManagerThread = new Thread(clientSocketManage);
              clientSocketManagerThread.start();
             System.out.println("DEBUG 0");
         } catch (Exception except) { // Any Failure will send 421 Error to client
             System.out.println("\t 421  \t Service not available, closing transmission channel.\n" + except);
         }
     }
 }

 static class ClientSocketManager implements Runnable {

 public void run() {

     try {

         String CRLF = "\r\n";
         String LF = "\n";
         boolean SocketInitiation = false;
         socketManager soketManager = null;

         DataInputStream clientDataIn = new DataInputStream(soketManager.getInputStream());
         socketManager clientReaderSocket = soketManager;

         DataOutputStream clientDataOut;
         clientDataOut = new DataOutputStream(soketManager.getOutputStream());

         String sendSocketMessage = (CRLF);
         clientDataOut.writeUTF(sendSocketMessage);// Sends string to output stream using UTF-8
         clientDataOut.flush();
         System.out.println("DEBUG 1");

         String socketReplyIn =  clientDataIn.readUTF();

         PortManager portInstance = new PortManager();
         int portNumber = portInstance.serverPort;
         Socket soket = new Socket("loopback", portNumber);

         ClientWriter clientWrite = new ClientWriter(soket);
         Thread clientWriteThread = new Thread(clientWrite);

         ClientReader clientRead = new ClientReader(soket);
         Thread clientReadThread = new Thread(clientRead);

         System.out.println("DEBUG 2");
         // Cleans stream from any write buffer method.
         System.out.println("Connection to server using TCP...");

         if (socketReplyIn.contains("220")) {
             System.out.println("\t 220 \t Service ready"); // Connection established successfully

             clientReadThread.start();
             clientWriteThread.start();

             SocketInitiation = true;
         } else {
             System.out.println("\t 421 \t Service not available, closing transmission channel");
             SocketInitiation = false;
         }

     } catch (IOException e) {
         System.out.println("\t 421 \t Service not available, closing transmission channel");
         System.out.println("TCP connection error: " + e);
     }



 }

}


Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
jawaxig298
  • 1
  • 1
  • 5

1 Answers1

1

you have defined socketManager soketManager = null; in ClientSocketManager. but you never assigned a value to it, so it is still null.

The code after that is trying to access streams from it, which is throwing NullPointerException:

         DataInputStream clientDataIn = new DataInputStream(soketManager.getInputStream());
         socketManager clientReaderSocket = soketManager;

         DataOutputStream clientDataOut;
         clientDataOut = new DataOutputStream(soketManager.getOutputStream());

         String sendSocketMessage = (CRLF);
         clientDataOut.writeUTF(sendSocketMessage);// Sends string to output stream using UTF-8
         clientDataOut.flush();
         System.out.println("DEBUG 1");

just create a new instance of the socketManager and assign it to soketManager before using it.

PortManager portInstance = new PortManager();
int portNumber = portInstance.serverPort;
Socket soket = new Socket("loopback", portNumber);

socketManager soketManager = new sockerManager(soket);
Harshvardhan Joshi
  • 2,855
  • 2
  • 18
  • 31
  • How can i do that? Could provide an example pretty please? – jawaxig298 Dec 25 '19 at 03:48
  • I don't know what `socketManager` class is but you can do something like this. Instead of assigning `nulll` => `socketManager soketManager = new socketManager();` – Harshvardhan Joshi Dec 25 '19 at 03:51
  • ``` public socketManager(Socket socket) throws IOException { soc = socket; input = new DataInputStream(soc.getInputStream()); output = new DataOutputStream(soc.getOutputStream()); }```` – jawaxig298 Dec 25 '19 at 03:53