1

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) Always copy/paste error and exception output! 2) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 3) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Feb 06 '15 at 01:06
  • Could you specify which class 'this' is pointing to? – Prudhvi Feb 06 '15 at 01:06
  • this is pointing to the ClientHandler Class –  Feb 06 '15 at 01:08
  • Could you update your post? Try to post entire code and the exception output. It would be easy to figure out the problem then. – Prudhvi Feb 06 '15 at 01:11

0 Answers0