This code works, because while loop is working, but when I remove this comment in UserThread class
/* for(Entry<Integer,List<String>> m: server.lista.entrySet()) {
if(c==m.getKey()) {
m.getValue().add(name);
//toUser.println("");
}
}
for(Entry<Integer,List<String>> m: server.lista.entrySet()) {
System.out.println(m.getKey()+" "+m.getValue());
}*/
client doesn't receive next question. While loop is stopping here. Can I get reason why this is happening? Codes are below.
package quiz;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
public class Server {
public static int test_port=12321;
private int port;
private int counter=0;
private int length;
BufferedReader fromDocument;
public int numberOfClients=0;
public static HashMap<Integer,List<String>> lista=new HashMap<Integer,List<String>>();
public static void main(String[] args) {
//int l=lengthHashMap();
// TODO Auto-generated method stub
Server server=new Server(test_port);
int l=server.lengthHashMap();
for(int i=1;i<l;i++) {
lista.put(i, null);
}
server.execute();
}
private void execute() {
// TODO Auto-generated method stub
try(ServerSocket server=new ServerSocket(port)){
while(true) {
Socket client=server.accept();
System.out.println("Client connected");
numberOfClients++;
UserThread user=new UserThread(client,this);
user.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Server(int port) {
this.port=port;
}
private int lengthHashMap() {
try {
fromDocument=new BufferedReader(new InputStreamReader(new FileInputStream("Questions.txt")));
String line=fromDocument.readLine();
while(line!=null) {
counter++;
line=fromDocument.readLine();
}
length=counter/5;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
return length;
}
}
}
package quiz;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.List;
import java.util.Map.Entry;
public class UserThread extends Thread{
Socket client;
Server server;
BufferedReader fromDocument;
private int counter=0;
PrintWriter toUser;
BufferedReader fromUser;
String correctAnswer;
String answer;
String correctedLine;
private int c=0;
public UserThread(Socket client, Server server) {
// TODO Auto-generated constructor stub
this.client=client;
this.server=server;
}
public void run() {
try {
while(server.numberOfClients<2) {
System.out.println();
}
fromDocument=new BufferedReader(new InputStreamReader(new FileInputStream("Questions.txt")));
fromUser=new BufferedReader(new InputStreamReader(this.client.getInputStream()));
toUser=new PrintWriter(this.client.getOutputStream(),true);
String line=fromDocument.readLine();
String name=fromUser.readLine();
System.out.println(name);
while(!line.equalsIgnoreCase("")) {
while(counter<5) {
counter++;
if(line.endsWith("*")){
correctAnswer=line.substring(0,1);
correctedLine=line.substring(0,line.length()-1);
toUser.println(correctedLine);
}
else {
toUser.println(line);
}
line=fromDocument.readLine();
}
c++;
counter=0;
answer=fromUser.readLine();
if(answer.equalsIgnoreCase(correctAnswer))
toUser.println("Correct!");
else
toUser.println("Incorrect!");
//line=fromUser.readLine();
//System.out.println(line);
/* for(Entry<Integer,List<String>> m: server.lista.entrySet()) {
if(c==m.getKey()) {
m.getValue().add(name);
//toUser.println("");
}
}
for(Entry<Integer,List<String>> m: server.lista.entrySet()) {
System.out.println(m.getKey()+" "+m.getValue());
}*/
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
package quiz;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
private String name;
private String hostname;
private int port;
String answer;
BufferedReader fromServer;
PrintWriter toServer;
public static void main(String[] args) {
// TODO Auto-generated method stub
Client client=new Client("localhost",Server.test_port);
client.execute();
}
private void execute() {
try {
this.setName();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
try(Socket client=new Socket(this.hostname,this.port)){
fromServer=new BufferedReader(new InputStreamReader(client.getInputStream()));
toServer=new PrintWriter(client.getOutputStream(),true);
System.out.println("Connected to server: "+this.hostname);
toServer.println(this.name);
String line;
while(true) {
for(int i=0;i<5;i++) {
line=fromServer.readLine();
System.out.println(line);
}
System.out.println("Your answer is: ");
Scanner sc=new Scanner(System.in);
answer=sc.nextLine();
toServer.println(answer);
line=fromServer.readLine();
System.out.println(line);
//toServer.println("Next");
//for(int j=0;j<5;j++)
// line=fromServer.readLine();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Client(String hostname,int port) {
this.hostname=hostname;
this.port=port;
}
private void setName() throws IOException {
// TODO Auto-generated method stub
System.out.println("Enter your name: ");
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
this.name=stdin.readLine();
}
}
Content of file Questions.txt:
1. Ko je režirao film Kad jaganjci utihnu?
A) Robert Zemekis
B) Frenk Darabont
C) Džonatan Dem *
D) Rob Rajner
2. U kojoj državi se nalazi jezero Retba?
A) U Senegalu *
B) U Ugandi
C) U Gani
D) U Nigeriji
3. Kako se zove drugi studijski album grupe Zabranjeno pušenje?
A) Dok čekaš sabah sa šejtanom *
B) Male priče o veliko ljubavi
C) Pozdrav iz zemlje Safari
D) Fildžan viška
4. Pod kojom zastavom nastupa teniser Alehandro Davidovič Fokina?
A) Pod zastavom Argentine
B) Pod zastavom Rusije
C) Pod zastavom Velike Britanije
D) Pod zastavom Španije *
A NullPointerException occurs in your code at the line m.getValue().add(name)
. The root cause of this exception is in your Server class. Observe the following code from your Server class:
for (int i = 1; i < l; i++) {
lista.put(i, null);
}
You associate each Integer to a null value. Now, whenever you will use lista.get(1)
it returns null. What happens when you use lista.get(1).add(name)
? It throws a NullPointerException. That is why m.getValue().add(name)
throws NullPointerException, since m.getValue()
is null.
Your code is filled with bad practices. You should avoid them as this makes code harder to debug, less readable and can even impact performance. Here are few things that I have observed:
You did not print the stack trace in your UserThread class. You would have caught the problem by yourself if you would have done so. Currently, your catch block is just empty.
port
variable can be final and static. Server will work on a fixed port and it is associated to a server as a whole and not a particular instance of server object. Hence, it can be final and static.
Returning from finally block. This is a very bad practice and for several reasons. You can search about it online to know more.
While statement could not complete without throwing exception. Your loop is infinite and it can only end whenever an exception occurs. You can add graceful ending at the client. It can finish whenever it has answered all the questions.
There could be few other minor improvements but I will keep the list short.