Hello I am writing a client server code in Java. I am trying to provide the server the facility to add some values to a list. I am writing the following code for giving the server to take input
while (true) {
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
boolean isAdd=false;
String str=" ";
System.out.println("Do the server want to give input? yes/no");
//sc.next();
if(sc.nextLine().equals("yes")){
System.out.println("Enter The value:");
str=sc.nextLine();
System.out.println("To be Added or Removed? add/remove");
if(sc.nextLine().equals("add")){
isAdd=true;
}
}
sc.close();
System.out.println(str);
Thread t = new Thread(new ClientHandler(clientSocket, str.toString(),isAdd));
t.start();
And the following code is inside the run method
this.str=str;
System.out.println(str);
if(str!=" " && isAdd)
this.addElement(str);
if(str!=" " && !isAdd)
this.removeElement(str);
Path path=Paths.get("companies.txt");
try {
this.l=(ArrayList<String>) Files.readAllLines(path);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But the output is showing null pointer exception at the lines
Thread t = new Thread(new ClientHandler(clientSocket, str.toString(),isAdd));
if(str!=" " && isAdd)
this.addElement(str);
if(str!=" " && !isAdd)
this.removeElement(str);
I am totally stack why is it happening? Can anyone help me? I also checked the values at different points. No null value is coming.