Problems with FXML Task and Progressbar

Asked

Viewed 74 times

1

I want to open a screen in Javafx that will load a search in the bank and bring to a Table. I would like this search to be done by another Thread, since it may take a while and would like to update a ProgressBar with the process of Thread.

I made this code that is returning me NullPointerException for my fxml attributes. I’m still having doubts how the initialize of the classes Controller, maybe this is the problem.

For fxml I use Scene Builder.
This is the method of my class UsuarioController which loads the Logs.fxml file:

private void carregarLogsDoUsuario(){
    LogsController.setCodUsuario(usuario.getCodUsuario());
    try {
        Parent p = fxmlLoader.load(getClass().getResource(TelasFramework.LOGS_FXML));

        Stage s = new Stage();
        Scene cena = new Scene(p);
        s.setScene(cena);
        //this.controladorDeTela.getPalco().setScene(cena);
        s.show();

    } catch (IOException ex) {
        FabricaDeDialogos.montaDialogoErro(ex, "Carregando Logs do usuário:");
        ex.printStackTrace();
    }
}

And this is the class LogsControlles that controls fxml and would carry out the process:

public class LogsController implements Initializable {

    //<editor-fold desc="FXML Scene Builder">
    @FXML
    private AnchorPane painelPrincipal;
    @FXML
    private TableView<Logs> tbLogs;
    @FXML
    private TableColumn<Logs, String> colunaAcao;
    @FXML
    private TableColumn<Logs, String> colunaData;
    @FXML
    private TableColumn<Logs, String> colunaHora;
    @FXML
    private ProgressBar barraDeProgresso;
    @FXML
    private Label labelInfo;
    //</editor-fold>

    private static int codUsuario;
    UsuarioDAO usuDao = new UsuarioDAO();

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        System.out.println("Chamando método carregarLogs();");
        carregarLogs();
    }

    private void carregarLogs() {
        Task<Void> task = new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("Chamou método call();");
                        updateMessage("Carregando Logs do usuários: ");
                        barraDeProgresso.setProgress(0);
                        try {

                            colunaAcao.setCellValueFactory(new PropertyValueFactory<>("acao"));
                            colunaData.setCellValueFactory(new PropertyValueFactory<>("data"));
                            colunaHora.setCellValueFactory(new PropertyValueFactory<>("hora"));

                            barraDeProgresso.setProgress(getTotalWork() - getWorkDone());

                            tbLogs.setItems(FXCollections.observableList(usuDao.listarLogsAcaoDoUsuario(codUsuario)));

                            barraDeProgresso.setProgress(getTotalWork() - getWorkDone());

                            updateMessage("Logs carregados.");

                            System.out.println("Logs Carregados");

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            FabricaDeDialogos.montaDialogoErro(e, "Thread de carregamento de Logs interrompida.");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                            FabricaDeDialogos.montaDialogoErro(ex, "Thread de carregamento de Logs interrompida.");
                        }
                    }
                });
                return null;
            }
        };
        labelInfo.textProperty().bind(task.messageProperty());
        barraDeProgresso.progressProperty().bind(task.progressProperty());
        new Thread(task).start();
    }

    public static int getCodUsuario() {
        return codUsuario;
    }

    public static void setCodUsuario(int codUsuario) {
        LogsController.codUsuario = codUsuario;
    }
}
  • Marcelo, first welcome to Stackoverflow. Could edit your question and include the generated exception log? If possible, also edit the code and make a comment on the line you are generating the NullPointerException.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.