0

This code works fine, I mean the multi thread part.. the only problem is that I get a NPE just only if I call save method

Exception in thread "pool-1-thread-1" java.lang.NullPointerException at app.Controller.handleClient(Controller.java:54) at app.Controller.run(Controller.java:109) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

How can I handle this? Thank you

coding section

BlockingQueue<Client> clients_list;

ExecutorService executor = Executors.newFixedThreadPool(10);

for (Client client: clients_list) {
        Runnable worker = new Controller(client);
        executor.execute(worker);
}

public Controller(Client client) {
        this.client = client;
}

// Controller class implements Runnable, here run method @Override public void run() { // do some stuff on client // then save data into a file handleClient(client); }

private void handleClient(Client client) {
    String c_rwx = client.getPermission()
    if(c_rwx.contains("allowed to:")) {
       client.setUsername = client.getUsername() + " allowed" ;
       save(client);
    }
}

private void save(Client client) {
        FileWriter fw = null;
        try {
            File file = new File("done.txt");
            if (!file.exists()) {
                file.createNewFile();
            }
            fw = new FileWriter(file, true);
            fw.write("client info:" + client.getUsername() + " \n");
            // ...
            fw.flush();
        } catch (IOException ex) {
            System.out.println("err saving file");
        }
}

FIXED EVERYTHING added after the for

executor.shutdown();

0 Answers0