I'm trying to create a multicolor table (composed by buttons) where every X seconds a thread change the color of its button. The first thing I did was manage the threads with a semaphore, but then I realized that maybe it wasn't necessary since I want that each button changes its color independently from the others. This is my code:

public class Thread_ES2 extends Application implements Runnable {

private Button[] btn = new Button[4];
private Semaphore sem = new Semaphore(1);

public void run() {
    //while (true) {
        try {
            //sem.acquire();
            if (Thread.currentThread().getId() % 4 == 0) {
                btn[0].setStyle("-fx-background-color: #FF7F50");
                Thread.sleep(2000);
            } else if (Thread.currentThread().getId() % 3 == 0) {
                btn[1].setStyle("-fx-background-color: #00ff00");
                Thread.sleep(5000);
            } else if (Thread.currentThread().getId() % 2 == 0) {
                btn[2].setStyle("-fx-background-color: #FF6347");
                Thread.sleep(1000);
            } else {
                btn[3].setStyle("-fx-background-color: #FF4500");
                Thread.sleep(6000);
            }
        } catch (InterruptedException e) {
            System.out.println("Errore");
        } finally {
            //sem.release();
        }
    //}
}

@Override
public void start(Stage primaryStage) throws IOException {
    GridPane root = FXMLLoader.load(getClass().getResource("button.fxml"));

    for (int i = 0; i < 4; i++) {
        btn[i] = new Button();
    }

    btn[0].setId("bt1");
    btn[1].setId("bt2");
    btn[2].setId("bt3");
    btn[3].setId("bt4");

    root.getChildren().add(btn[0]);
    root.getChildren().add(btn[1]);
    root.getChildren().add(btn[2]);
    root.getChildren().add(btn[3]);

    Scene scene = new Scene(root);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
    Thread_ES2 mc = new Thread_ES2();
    Thread t1 = new Thread(mc);
    Thread t2 = new Thread(mc);
    Thread t3 = new Thread(mc);
    Thread t4 = new Thread(mc);
    t1.start();
    t2.start();
    t3.start();
    t4.start();
}

This is the error I get:

Exception in thread "Thread-6" Exception in thread "Thread-8" java.lang.NullPointerException at thread_es2.Thread_ES2.run(Thread_ES2.java:44) at java.lang.Thread.run(Thread.java:748) Exception in thread "Thread-7" java.lang.NullPointerException at thread_es2.Thread_ES2.run(Thread_ES2.java:44) at java.lang.Thread.run(Thread.java:748) java.lang.NullPointerException at thread_es2.Thread_ES2.run(Thread_ES2.java:38) at java.lang.Thread.run(Thread.java:748) Exception in thread "Thread-5" java.lang.NullPointerException at thread_es2.Thread_ES2.run(Thread_ES2.java:35) at java.lang.Thread.run(Thread.java:748)

Your approach to this is somewhat confusing - why not 4 timers each handling the color change for one button?
Anyway, the NPE may be related to the button arrays - that's arrays, not array, because you have two of them, one in the Thread_ES2 instance started by JavaFX when the program starts, and one in the instance created by you on line 54 above. You initialise the first array, but the second is still full of nulls.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.