I'm trying to build a client/server application in which the client gives commands to the server and depending on the command, the server executes different functionalities.
My client codes goes as follows:
public class Client {
private Socket clientSocket;
private BufferedReader buffer;
private BufferedReader in;
private PrintWriter out;
public Client (String host, int port) throws UnknownHostException, IOException {
this.clientSocket = new Socket (host,port);
this.buffer = new BufferedReader(new InputStreamReader (System.in));
}
public void start () throws IOException {
this.in = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
this.out = new PrintWriter (this.clientSocket.getOutputStream());
while (true ) {
String input = buffer.readLine();
String parser [] = input.trim().split(":");
this.out.println(input);
this.out.flush();
if ("quit".equals(parser[0]) || parser[0] == null)
break;
else if ("create account".equals(parser[0]))
System.out.println(in.readLine());
else if ("upload".equals(parser[0])) {
System.out.println("Here");
File file = new File(parser[1]);
long length = file.length();
byte[] bytes = new byte[1000000];
InputStream in = new FileInputStream(file);
OutputStream out = this.clientSocket.getOutputStream();
int count;
System.out.println("Sending");
while ((count = in.read(bytes)) > 0) {
out.write(bytes, 0, count);
}
out.close();
in.close();
break;
}
}
}
public static void main (String args []) throws UnknownHostException, IOException {
Client client = new Client ("127.0.0.1", 6666);
client.start();
}
}
My server code goes as follows:
public class Server {
public static void main (String args []) throws IOException {
ServerSocket socket = new ServerSocket (6666);
Biblioteca biblioteca = new Biblioteca(); //biblioteca comum a todas as threads
while (true) {
Socket clientSocket = socket.accept();
Thread cliente = new Thread (new Service (clientSocket, biblioteca));
cliente.start();
}
}
}
Since this is intended to be a multi client server, i created a thread that runs the entire thing. Here is the code for it:
public class Service implements Runnable {
private Socket clientSocket;
private Biblioteca biblioteca;
public Service (Socket clientSocket, Biblioteca biblioteca) throws IOException {
this.clientSocket = clientSocket;
this.biblioteca = biblioteca;
}
public void run () {
String answer;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(this.clientSocket.getOutputStream());
while (true) {
String s = in.readLine();
String parser [] = s.trim().split(":");
if ("create account".equals(parser[0])) {
this.biblioteca.createAccount(parser[1], parser[2]);
out.println("Conta com email: " + parser[1] + " criada\n");
out.flush();
}
else if ("upload".equals(parser[0])){
InputStream inS = this.clientSocket.getInputStream();
OutputStream outS = new FileOutputStream(new File ("/Users/Jota/Desktop/SDSERVER/test2.xml"));
byte[] bytes = new byte[1000000];
int count;
System.out.println ("Receiving");
while ((count = inS.read(bytes)) > 0) {
outS.write(bytes, 0, count);
}
System.out.println("Received");
outS.close();
inS.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
So everything runs as planned, except when i try to use the "upload" functionality. On the client command line, if i type something like "upload:/Users/Jota/Desktop/SDMUSICA/encostateamim.mp3", it is supposed to be parsed this way: parser[0] == "upload" and parser[1] == "/Users/Jota/Desktop/SDMUSICA/encostateamim.mp3"
The problem is i get the following error:
java.lang.NullPointerException
at Service.run(Service.java:31)
And the line 31 of the service class is:
String parser [] = s.trim().split(":");
I can't understand how there can be a nullPointerException here, can someone help me?