I'm developing a Spring + Java Fx application, when calling the code from the main class screen, it works normally, but if I call another screen, it does not write the information in the model and gives null pointer exception, how I managed the screens and it did not work, it still gives the same error ... could anyone help me?
This is the code for the main class, where it is working normally:
@SpringBootApplication(scanBasePackages = { "br.com.cron.folhapagamento.service",
"br.com.folhapagamento.controller", "br.com.folhapagamento.main" })
@EntityScan(basePackages = { "br.com.folhapagamento.model" })
@EnableJpaRepositories(basePackages = { "br.com.folhapagamento.repository" })
public class FolhaDePagamentoApplication extends Application {
private static final Logger log =
LoggerFactory.getLogger(FolhaDePagamentoApplication.class);
private ConfigurableApplicationContext context;
public static void main(String[] args) {
launch(args);
}
@Override
public void init() throws Exception {
super.init();
SpringApplicationBuilder builder = new SpringApplicationBuilder(FolhaDePagamentoApplication.class);
context = builder.run(getParameters().getRaw().toArray(new String[0]));
}
@Override
public void start(Stage primaryStage) throws Exception {
log.info("Starting...");
FXMLLoader loader = new
FXMLLoader(getClass().getResource("../view/LeiAutorizativa.fxml"));
loader.setControllerFactory(context::getBean);
Parent root = loader.load();
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass()
.getResource("stylesheet.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("TableView App");
primaryStage.show();
}
}
And here the class I manage the screens, and it's the way it is not working, I wanted it to work normal:
private static AplicacaoUtil instancia = null;
private Stage telaAtual = null;
private AplicacaoUtil() {
this.telaAtual = new Stage();
}
public static AplicacaoUtil getInstancia()
{
if(instancia == null){
instancia = new AplicacaoUtil();
}
return instancia;
}
public void irParaTela(String nomeTela) {
try {
System.out.println("Carrega o arquivo da tela desejada");
Parent root = FXMLLoader.load(getClass().getResource("../view/".concat(nomeTela)));
System.out.println("Cria uma nova cena para a tela e adiciona no palco (telaAtual)");
Scene scene = new Scene(root);
this.telaAtual.setScene(scene);
System.out.println("Exibe o palco caso o mesmo não esteja sendo exibido");
if(!this.telaAtual.isShowing()){
this.telaAtual.show();
}
}catch(Exception e){
System.out.println("entrou no catch");
System.err.println("Ocorreu um erro ao tentar navegar para tela: ".concat(nomeTela).concat(" ".concat(e.getMessage())));
//exibe uma mensagem caso a tela não tenha sido encontrada + erro original
}
}
public void setTelaAtual(Stage telaAtual) {
this.telaAtual = telaAtual;
}
public Stage getTelaAtual() {
return telaAtual;
}
It manages the screens through a Singleton, and to call the screens I call the irParaTela
method and I pass the String that will be the name of the screen, it calls, however when sending the information to the bank it gives error .
I tried to do this also to save the use of an auxiliary class, and I thought it would work, but it did the same thing. :
TelasFx telas = new TelasFx();
private static final Logger log = LoggerFactory.getLogger(FolhaDePagamentoApplication.class);
private ConfigurableApplicationContext context;
public static void main(String[] args) {
launch(args);
}
@Override
public void init() throws Exception {
super.init();
SpringApplicationBuilder builder = new SpringApplicationBuilder(FolhaDePagamentoApplication.class);
context = builder.run(getParameters().getRaw().toArray(new String[0]));
}
@Override
public void start(Stage primaryStage) throws Exception {
log.info("Starting...");
FXMLLoader loader = new FXMLLoader(getClass().getResource("../view/" .concat(telas.getNomeTela())));
loader.setControllerFactory(context::getBean);
Parent root = loader.load();
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("stylesheet.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("TableView App");
primaryStage.show();
}
public void vaiParaTela() throws Exception {
init();
Stage primaryStage = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("../view/" .concat(telas.getNomeTela())));
loader.setControllerFactory(context::getBean);
Parent root = loader.load();
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("stylesheet.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("TableView App");
primaryStage.show();
}