-1

I am writing a simple Client Server program in java. Client sends a filename to server. Server receives it, then parses it and after parsing the filename it sends response to client wheather filename is correct or invalid.

In server, when it parses the filename it throws NullPointerException error.

Here is the code:

Server.java:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;


public class Server extends Thread {

private static int port;
private ServerSocket serverSocket=null;
private String ReceivedFilename=null;
private String ServerResponse=null;

public Server(int port) throws IOException {
    serverSocket = new ServerSocket(port);
    //serverSocket.setSoTimeout(20000);
}

public void run() {
    while (true) {
        try {
            System.out.println("Waiting for client to send File name...");
            Socket server = serverSocket.accept();
            System.out.println("Connected to Client");
            DataInputStream in = new DataInputStream(server.getInputStream());
            ReceivedFilename = in.readUTF();
            System.out.println("Received Filename: "+ReceivedFilename);
            if(ReceivedFilename == "")
                ServerResponse = "Filename is empty!";
            else{
                if(ServerResponse.indexOf(".bat") == ServerResponse.length()-4)
                {
                    ServerResponse = "Correct file name provided.";
                }
                else
                {
                    ServerResponse = "Invalid Filename! Server only accepts .bat files";
                }
            }
            DataOutputStream out = new DataOutputStream(server.getOutputStream());
            out.writeUTF(ServerResponse);
            server.close();
        } catch (SocketTimeoutException s) {
            System.out.println("Socket timed out!");
            break;
        } catch (IOException e) {
            e.printStackTrace();
            break;
        }
    }
}

public static void main(String[] args) {
    port = 9000;
    try {
        Thread t = new Server(port);
        t.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

Client.java:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Client {

private static String serverName;

public static void main(String[] args) {
    String sName = "localhost";
    int port = 9000;
    String Filename = "Scripts.bat";
    try {
        System.out.println("Connecting to Server");
        Socket client = new Socket(sName, port);
        System.out.println("Connected to Server");
        OutputStream outToServer = client.getOutputStream();
        DataOutputStream out = new DataOutputStream(outToServer);
        System.out.println("Sending filename to Server");
        out.writeUTF(Filename);
        System.out.println("Filename sent");
        InputStream inFromServer = client.getInputStream();
        DataInputStream in = new DataInputStream(inFromServer);
        System.out.println("Server says " + in.readUTF());
        client.close();
    } catch (IOException e) {
    }
}
}

Following is the output and exception on Server:

enter image description here

Please help out.

Azeem
  • 2,904
  • 18
  • 54
  • 89
  • 1
    Which one is line 44 in Server.java? – Sambhav Sharma Jul 02 '14 at 10:34
  • Line44 is empty. Line 43 is: `if(ServerResponse.indexOf(".bat") == ServerResponse.length()-4)` – Azeem Jul 02 '14 at 10:35
  • 1
    You should use : `if(ReceivedFilename == null || "".equals(ReceivedFilename.trim()))`. Oh, and also you should invest a little of your time in learning how to debug. That way, you could find yourself what's causing a NullPointerException – Daniel Jul 02 '14 at 10:39
  • Yes, if you are using an IDE like eclipse, you must learn to debug your code with a debugger, its pretty simple using IDEs. – Sambhav Sharma Jul 02 '14 at 10:44
  • 2
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Oleg Estekhin Jul 02 '14 at 10:46
  • 1
    In particular, this answer explains how to figure out what cause the NPE: http://stackoverflow.com/a/24347569/139985 – Stephen C Jul 02 '14 at 10:56

2 Answers2

1
ServerResponse=null

You dont set ServerResponse anywhere apart from in your if branch, but it looks like it will go straight to your else branch where you call ServerResponse.indexOf - it is still null at this point.

Also, you should name your variables starting with a lowercase, ServerResponse should be serverResponse.

Stu Whyte
  • 758
  • 5
  • 19
1

The ServerResponse is null as the if condition to check the ReceivedFilename assigns a default value only when the ReceivedFilename is blank.

if(ReceivedFilename.equals("")) // I changed it to use equals method
    ServerResponse = "Filename is empty!"; // only here the default value is assigned

You need to assign the Receivedfilename to your ServerResponse looking at the logic in the code.

if(ReceivedFilename.equals("")) // I changed it to use equals method
    ServerResponse = "Filename is empty!";
else{
     // If it comes to the else part, the ServerResponse is still null as initialized at the start and therefore throws the NPE
     ServerResponse = ReceivedFilename; // incase ReceivedFilename is not blank
     if(ServerResponse.indexOf(".bat") == ServerResponse.length()-4)
     ...
}

As a side note, you need to use equals() method to compare String values and not use the == operator which compares the String object references. You could probably trim() the ReceivedFilename when trying to check if its equals with blank String, but that's upto you. Ofcourse you've many different ways to check if a String is empty or not. You can either right the method to do that yourself or take help from third-party libraries which already have that method.

Rahul
  • 44,383
  • 11
  • 84
  • 103