0

I have a lab to do for my courses that consists of a trivial pursuit. After making the menu, we added buttons to switch between different views. I have a view in my application that allows you to edit questions (Json and tableView), but here is when you click on the button to open the map management view, there is a small lag that is due to creation of game cards. So, after some research on the internet, I came across the tasks in java, which I tried to apply but I am faced with an error. there she is :

enter image description here

There is my code :

public Deck getDeckCreate() {
    if(newDeck == null) {
        newDeck = new Deck();
        //Read Json File
        Gson gson = new Gson();
        String res  = JsonTransform.readFromFile("json.json", false);
        newDeck = gson.fromJson(res, Deck.class);
        //Read Json File
    }
    return newDeck;
}

public void createDeckTask() {
    //Task for computing the Panels:
    Task<ObservableList<Question>> task = new Task<ObservableList<Question>>() {
        @Override
        protected ObservableList<Question> call() throws Exception {
            return getDeck();
        }

    };

    // make sure the result will be assigned to the items property
    // on the application thread as soon as it's ready
    view.itemsProperty().bind(task.valueProperty());

    new Thread(task).start();
}

public GestionAdmin() {
    createDeckTask();
    createTable();

     }

Thank in advance ;)

fabian
  • 80,457
  • 12
  • 86
  • 114
  • Actually this line is problematic `view.itemsProperty().bind(task.valueProperty());`. You cannot bind to null and as the task is not yet started, the valueProperty is null. You should use the power of task : . `task.setOnSucccedded(e -> {view.getItems().addAll(task.getValue())});` or something like that. – Pagbo Mar 21 '18 at 09:29
  • 1
    @Pagbo If you could not bind to a property containing `null` the origin of the exception would be inside the `bind` method. `TableView` (see his last question https://stackoverflow.com/questions/49395286/task-javafx-creating-tableview-with-json) allows `null` as items though. `view` must be `null`. – fabian Mar 21 '18 at 09:36
  • Probably swapping `createDeckTask();` and `createTable();` will fix this. – fabian Mar 21 '18 at 09:51
  • You are right @fabian. My mistake. – Pagbo Mar 21 '18 at 09:52

0 Answers0