1
I am 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 record the information in the model and gives null Pointer Exception, I tried to change the way I manage the screens and it didn’t work, keeps giving the same error... someone could help me?
This is the code of 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’s the class I manage the screens in, and it’s the way it’s 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;
}
She manages the screens through a Singleton, and to call the screens I call the method irParaTela
and step to String that will be the name of the screen, it calls, but when sending save the information in the bank it gives error.
I tried to do this also to spare the use of an auxiliary class, and I thought it would work, but it gave 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();
}
I tried new modifications, now I’m trying to do everything for the main class.
– cloud Andrade