Reputation: 387
I'm getting those exceptions while trying to send a command from client to server:
Exception in thread "Thread-0" java.lang.NullPointerException
at pl.polsl.java.lab.server.ServerModel.reactForRequest(ServerModel.java:17)
at pl.polsl.java.lab.server.SingleService.run(ServerControler.java:82)
My server classes: Server Controler code:
package pl.polsl.java.lab.server;
import java.net.*;
import java.io.*;
/**
* The main class of the server
*
* @author sliwkacz
*
*/
public class ServerControler {
/** port number */
static final int PORT = 9997;
/**
* The main application method
*
* @params args all parametres are ignored
*/
public static void main(String args[]) throws IOException {
ServerSocket server = new ServerSocket(PORT);
System.out.println("Server started");
try {
while (true) {
Socket socket = server.accept();
try {
new SingleService(socket);
} catch (IOException e) {
socket.close();
System.err.println(e.getMessage());
}
}
} finally {
server.close();
}
}
}
/**
* The server class servicing a single connection
*/
class SingleService extends Thread {
/** socket representing connection to the client */
private Socket socket;
/** buffered input character stream */
private BufferedReader in;
/** Formatted output character stream */
private PrintWriter out;
/**
* The constructor of instance of the SingleService class. Use the socket as a parameter.
*
* @param socket socket representing connection to the client
*/
public SingleService(Socket socket) throws IOException {
this.socket = socket;
out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())), true);
in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
start();
}
/**
* Realizes the service
*/
boolean first=true;
@Override
public void run() {
try {
out.println("Type 'help' to get command list...");
while (true) {
String str = in.readLine();
if(!first){
String answer = ServerModel.reactForRequest(str);
if (str.equalsIgnoreCase("quit")) {
out.println("Server answers: Goodbye!");
break;
}
out.println("Server answers: " + answer);
System.out.println("Client sent: " + str);
}else{
out.println("HELLO!");
first=false;
}
}
System.out.println("closing...");
} catch (IOException e) {
System.err.println(e.getMessage());
} finally {
try {
socket.close();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
}
Server Model code:
package pl.polsl.java.lab.server;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
/**
* The class of the server's answers
*
* @author sliwkacz
*
*/
public class ServerModel {
public static String reactForRequest(String input){
String answer="";
if(input.equalsIgnoreCase("date")){
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date today = Calendar.getInstance().getTime();
answer = "Today's date is: "+df.format(today);
}else
if(input.equalsIgnoreCase("dayofayear")){
Calendar calendar = Calendar.getInstance();
int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
answer="Today is "+dayOfYear+" day of the year.";
}else
if(input.equalsIgnoreCase("daysleft")){
Calendar calendar = Calendar.getInstance();
int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
answer=""+(365-dayOfYear)+" days left this year.";
}else
if(input.equalsIgnoreCase("random")){
Random rand = new Random();
int number = rand.nextInt();
answer="This is a random numer: "+number;
}else
if(input.equalsIgnoreCase("help")){
answer="Try commands: date, dayofyear, daysleft, random.";
}
else answer="I don't know what you want from me... Type 'help' for command list.";
return answer;
}
}
My CLient class code:
package pl.polsl.java.lab.client;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
/**
* The main class of the client
*
* @author sliwkacz
*
*/
public class ClientView {
static final int PORT = 9997;
public static void main(String argv[]) throws Exception
{
System.out.println("Start communicating...");
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", PORT);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();`enter code here`
}
}
I can see the 'Server started' info while runnig the server project and the 'Start communicating...' info whiler runnig client project. Those errors pop up every time I try to send any kind of command from client to server. I'll be very greatful for some help.
Upvotes: 1
Views: 371
Reputation: 7129
The problem involves the while loop in ServerControler.run
You are calling:
String str = in.readLine();
But in
is a Scanner that reads from the socket, and because the other party has not sent anything yet, str
is null. Then you call reactForRequest
with str
as its argument, resulting in the NullPointerException. What you actually want to do, is read from the console.
Upvotes: 1