Error below are fixed by removing the declaration but another has appeared that did not previously exist.
Receiving two errors, any insight would be great, thanks.
Exception in thread “main” java.lang.NullPointerException
at ChatClient.(ChatClient.java:27)
at ChatClient.main(ChatClient.java:59)
From the following ChatClient:
import java.net.*;
import java.io.*;
public class ChatClient
{ private Socket socket = null;
private BufferedReader console = null;
private BufferedReader streamIn = null;
private DataOutputStream streamOut = null;
public ChatClient(String serverName, int serverPort, String userName)
{ System.out.println("Establishing connection. Please wait...");
try
{ socket = new Socket(serverName, serverPort);
System.out.println("Connected: " + socket);
System.out.println("CTRL+C or type .bye to quit");
start();
}
catch(UnknownHostException uhe)
{ System.out.println("Host unknown: " + uhe.getMessage());
}
catch(IOException ioe)
{ System.out.println("Unexpected exception: " + ioe.getMessage());
}
String line = "";
while (!line.equals(".bye"))
{ try
{ line = console.readLine();
streamOut.writeBytes(line + '\n'); //Send console data to server socket
String reply = streamIn.readLine(); //Recieve confirmation msg from server
System.out.println( reply ); //Print the msg
streamOut.flush();
}
catch(IOException ioe)
{ System.out.println("Sending error: " + ioe.getMessage());
}
}
}
public void start() throws IOException
{ console = new BufferedReader(new InputStreamReader(System.in)); //Changed console to BufferedReader
streamIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
streamOut = new DataOutputStream(socket.getOutputStream());
}
public void stop()
{ try
{ if (console != null) console.close();
if (streamOut != null) streamOut.close();
if (streamIn != null) streamIn.close(); //Is it good practice to close
if (socket != null) socket.close();
}
catch(IOException ioe)
{ System.out.println("Error closing ...");
}
}
public static void main(String args[])
{ ChatClient client = null;
if (args.length != 3)
System.out.println("Usage: java ChatClient host port username");
else
client = new ChatClient(args[0], Integer.parseInt(args[1]), args[2]);
}
}
and this error:
Exception in thread "Thread-1" java.lang.NullPointerException
at ChatServerThread.handleClient(ChatServerThread.java:41)
at ChatServerThread.run(ChatServerThread.java:17)
from ChatServerThread:
import java.net.*;
import java.io.*;
//public class ChatServerThread implements Runnable
public class ChatServerThread extends Thread
{ private Socket socket = null;
private ChatServer server = null;
private int ID = -1;
private BufferedReader streamIn = null;
private DataOutputStream streamOut = null;
public ChatServerThread(ChatServer _server, Socket _socket)
{ server = _server; socket = _socket; ID = socket.getPort();
}
public void run() {
try {
handleClient();
} catch( EOFException eof ) {
System.out.println("Client closed the connection.");
} catch( IOException ioe ) {
ioe.printStackTrace();
}
}
public void handleClient() throws IOException {
boolean done = false;
try {
System.out.println("Server Thread " + ID + " running.");
while (!done) {
String nextCommand = streamIn.readLine();
if( nextCommand.equals(".bye") ) {
System.out.println("Client disconnected with bye.");
done = true;
} else {
System.out.println( nextCommand );
String nextReply = "You sent me: " + nextCommand.toUpperCase() + '\n';
streamOut.writeBytes ( nextReply );
}
}
} finally {
streamIn.close();
streamOut.close();
socket.close();
}
}
public void open() throws IOException
{
streamIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
streamOut = new DataOutputStream(socket.getOutputStream());
}
public void close() throws IOException
{ if (socket != null) socket.close();
if (streamIn != null) streamIn.close();
if (streamOut != null) streamOut.close();
}
}
Yes, it’s this line:
consoleis still null. Even though you’re callingstart(), it doesn’t do what you think it does:This declares a new local variable called
console. It doesn’t change the value of the instance variable calledconsole. To do that, you should remove the declaration part:Even with that change, you could get problems – because if that does throw an exception, here’s what you’re doing with it in the constructor:
You’re then continuing as if nothing had happened. Don’t do that. You’re not really “handling” the exception – so you should almost certainly either not catch it in the first place, or rethrow it in your catch block.
As an aside, your bracing style is very dense, very non-conventional, and inconsistent. I would strongly recommend against including code following an opening brace (on the same line). As it is, your code is pretty hard to read for anyone used to either of the (vastly) more common conventions, of:
or