Antoine089
Antoine089

Reputation: 458

Client-Server Socket Programming Java

I'm new to socket programming and i'm trying to get my client-program to retrieve a url from a document on my desktop, send this url to my server-program, and get my server-program to send back the html code of this url-website as a string: I keep getting a NPE when i try to send back the string to my client, can anyone point me to what i'm doing wrong?

Server-Program:

 public class Server {
 public static void main(String args[]) {
    String fromClient;
    StringBuilder htmlCode = null;
    String str = null;
    try(ServerSocket welcomeSocket = new ServerSocket(8082)){
        System.out.println("Server started, waiting for clients...");
        while(true){
            try(Socket connectionSocket = welcomeSocket.accept();
                BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                DataOutputStream toClient = new DataOutputStream(connectionSocket.getOutputStream())){
                fromClient = inFromClient.readLine();
                try(InputStream url = new URL("http://"+fromClient+"/index.html").openStream();
                    BufferedReader getUrl = new BufferedReader(new InputStreamReader(url))){
                        for(String line = getUrl.readLine(); line != null; line = getUrl.readLine()){
                            //THIS IS WHERE ECLIPSE SAYS NPE IS HAPPENING
                            htmlCode = htmlCode.append(line);
                        }
                    str = htmlCode.toString();
                    toClient.writeBytes(str);   
                }
            }
        }
    }
    catch(IOException io){
        io.printStackTrace();
    }
}

}

Client-Side:

 public class Client {
 public static void main(String[] args) {
    String url;
    String returned;
    try(Socket clientSocket = new Socket("localhost", 8082);
        BufferedReader inFromUser = new BufferedReader(new FileReader("C:\\Users\\Anthony\\Desktop\\client.txt"));
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))){

        url = inFromUser.readLine();
        outToServer.writeBytes(url + "\n");
        returned = inFromServer.readLine();
        System.out.println("FROM SERVER: " + returned);
    }
    catch(IOException io){
        io.printStackTrace();
    }
}

}

Upvotes: 1

Views: 226

Answers (2)

Andy Thomas
Andy Thomas

Reputation: 86381

You're using htmlCode before you give it a non-null value.

StringBuilder htmlCode = null;

...

htmlCode = htmlCode.append(line);

You could instead initialize it up front:

StringBuilder htmlCode = new StringBuilder();
...
htmlCode = htmlCode.append(line);

Upvotes: 0

Hendrik Simon
Hendrik Simon

Reputation: 231

The problem lies in your Server:

You initialize the StringBuilder with null:

StringBuilder htmlCode = null;

and then try to call method on a reference which is null:

htmlCode = htmlCode.append(line);

Try

StringBuilder htmlCode = new StringBuilder();

instead. This should solve the NullPointerException.

Upvotes: 1

Related Questions