I am creating a an application where a client can send TCP packets to a server and the server will respond.However, when I flush() my buffered data to an outputstream, it produces a nullpointer exception. For each new client that connects, a new thread with a class is created. Here is the class:
public class clientHandler implements Runnable{
private boolean loggedin = false;
private String username = "";
Socket cs;
OutputStream rawoutput; //for some reason I had to pass the outputstream directly to the constructor rather than get it here, here it would produce a npe
clientHandler(Socket clientSocket, OutputStream bos){
rawoutput = bos;
cs = clientSocket;
}
DataOutputStream output;
{
try {
output = new DataOutputStream(rawoutput);
} catch (Exception e) {
System.out.println("rip OS");
e.printStackTrace();
}
}
//a couple more methods...
private void metaData(String data) throws Exception{
if(data.startsWith("/iusername")){
String[] useful = data.split(";");
System.out.println(useful[0].split(":")[1] + " logged in");
output.writeBytes("logged in");
output.flush();
username = useful[0].split(":")[1];
loggedin = true;
}
}
}
I am using netcat as a 'client' until I write my own. Is this perhaps why it causes the NPE? I have tried this all different kinds of outputstreams and writers and they all produce error at either the initialization, the write or the flush.