I'm having a problem to set a label from another controller, my project is structured as follows: FilmeOverviewController, is the overview that contains a tableview, in that tableview you can select the movie and etc, then I created a handle for a button that takes these attributes from the movies and there is another class called Room, in which there are some attributes (if it is 3d, capacity and etc) however, I can not in any way use the attributes contained in that other room, even initializing it, always I have NullPointer or something, does anyone know what I can do to fix this?
Here is the code for the MovieOverviewController class and the room class respectively:
public class FilmeOverviewController {
@FXML
private TableView<Filmes> filmeTable;
@FXML
private TableColumn<Filmes, String> nomeColumn;
@FXML
private TableColumn<Filmes, String> categoriaColumn;
@FXML
private TableColumn<Filmes, String> salaColumn;
@FXML
private Label nomeLabel;
@FXML
private Label salaLabel;
@FXML
private Label categoriaLabel;
@FXML
private Label diretorLabel;
@FXML
private Label duracaoLabel;
@FXML
private Label protagonistaLabel;
@FXML
private Label classificacaoLabel;
@FXML
private Label capacidadeLabel;
@FXML
private Label disponivelLabel;
@FXML
private Label is3dLabel;
@FXML
private JFXButton venderIngresso;
// referencia a classe main
private MainApp mainApp;
public FilmeOverviewController() {
}
@FXML
private void initialize() {
//Inicia a tableview com tres colunas.
nomeColumn.setCellValueFactory(cellData -> cellData.getValue().nomeProperty());
categoriaColumn.setCellValueFactory(cellData -> cellData.getValue().categoriaProperty());
salaColumn.setCellValueFactory(cellData -> cellData.getValue().getSala());
// limpando os detalhes
showFilmeDetails(null);
// adicionando funcao
filmeTable.getSelectionModel().selectedItemProperty()
.addListener((observable, oldValue, newValue) -> showFilmeDetails(newValue));
}
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
//adiciona uma observable list
filmeTable.setItems(mainApp.getfilmeDados());
}
private void showFilmeDetails(Filmes filme) {
if (filme != null) {
nomeLabel.setText(filme.getNome());
categoriaLabel.setText(filme.getCategoria());
duracaoLabel.setText(filme.getDuracao());
protagonistaLabel.setText(filme.getProtagonista());
classificacaoLabel.setText(filme.getClassificacao());
diretorLabel.setText(filme.getDiretor());
salaLabel.setText(filme.getSalaNumero());
capacidadeLabel.setText(Integer.toString(filme.getCapacidade()));
disponivelLabel.setText(Integer.toString(filme.getDisp()));
if (filme.get3D() == true) {
is3dLabel.setText("Sim");
} else {
is3dLabel.setText("Não");
}
} else {
nomeLabel.setText("");
categoriaLabel.setText("");
duracaoLabel.setText("");
protagonistaLabel.setText("");
classificacaoLabel.setText("");
diretorLabel.setText("");
salaLabel.setText("");
capacidadeLabel.setText("");
disponivelLabel.setText("");
is3dLabel.setText("");
}
}
@FXML
private void handleDeletarFilme() {
int selectedIndex = filmeTable.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
filmeTable.getItems().remove(selectedIndex);
} else {
Alert alerta = new Alert(AlertType.WARNING);
alerta.setTitle("Nenhum filme selecionado");
alerta.setHeaderText("Nenhuma Selecao");
alerta.setContentText("Por favor selecione um filme para deletar");
alerta.showAndWait();
}
}
@FXML
private void handleNovoFilme() {
Filmes tempFilme = new Filmes("Nome","Categoria");
boolean clicado = mainApp.showEditarFilmeDialog(tempFilme);
if (clicado) {
mainApp.getfilmeDados().add(tempFilme);
}
}
@FXML
private void handleEditarFilme() {
Filmes filmeSelecionado = filmeTable.getSelectionModel().getSelectedItem();
if(filmeSelecionado != null) {
boolean clicado = mainApp.showEditarFilmeDialog(filmeSelecionado);
if(clicado) {
showFilmeDetails(filmeSelecionado);
}
}else {
//se nada for selecionado
Alert alerta = new Alert(AlertType.WARNING);
alerta.initOwner(mainApp.getPrimaryStage());
alerta.setTitle("Nenhuma selecao");
alerta.setHeaderText("Nenhum filme selecionado");
alerta.setContentText("Por favor selecione algum filme.");
alerta.showAndWait();
}
}
@FXML
public void handleVenderIngresso() throws IOException {
Filmes filmeSelecionado = filmeTable.getSelectionModel().getSelectedItem();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("resources/VenderIngresso.fxml"));
AnchorPane pane = (AnchorPane) loader.load();
//criar o stage
Stage dialogStage = new Stage();
dialogStage.setTitle("Vender Ingresso");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(mainApp.getPrimaryStage());
Scene scene = new Scene(pane);
dialogStage.setScene(scene);
//chamando o controlador
VenderIngressoController controller = loader.getController();
controller.setDialogStage(dialogStage);
//mostrando o dialog e esperando ate fecharem
dialogStage.showAndWait();
}
}
public class Sala {
private boolean e3d;
private int assentosMax;
private int assentosDisp;
private final StringProperty numeroProperty = new SimpleStringProperty();
public Sala(boolean e3d, int assentosMax, int assentosDisp, String numero) {
setNumero(numero);
e3d = this.e3d;
assentosMax = this.assentosMax;
assentosDisp = this.assentosDisp;
}
public boolean is3d() {
return e3d;
}
public void setE3d(boolean e3d) {
this.e3d = e3d;
}
public int getAssentosMax() {
return assentosMax;
}
public void setAssentosMax(int assentosMax) {
this.assentosMax = assentosMax;
}
public int getAssentosDisp() {
return assentosDisp;
}
public void setAssentosDisp(int assentosDisp) {
this.assentosDisp = assentosDisp;
}
public StringProperty numeroProperty() {
return numeroProperty;
}
public final String getNumero() {
return numeroProperty.get();
}
public final void setNumero(String numero) {
numeroProperty().set(numero);
}
}
So I created a controller where I want to get the attributes of the selected movie and put it in the textfield:
public class VenderIngressoController {
@FXML
private Label tituloLabel;
@FXML
private Label salaLabel;
@FXML
private Label categoriaLabel;
@FXML
private Label diretorLabel;
@FXML
private Label duracaoLabel;
@FXML
private Label protagonistaLabel;
@FXML
private Label classificacaoLabel;
@FXML
private Label e3dLabel;
@FXML
private JFXButton gerarIngresso;
@FXML
private JFXButton cancelarIngresso;
@FXML
private CheckBox meiaEntradaBox;
@FXML
private JFXTextField precoEspecialField;
@FXML
private CheckBox checkPrecoEspecial;
private Filmes filme;
private Stage dialogStage;
private int precoEspecial;
public int getPrecoEspecial() {
return precoEspecial;
}
public void setPrecoEspecial(int precoEspecial) {
this.precoEspecial = precoEspecial;
}
public int getPreco() {
return preco;
}
public void setPreco(int preco) {
this.preco = preco;
}
public int getMeiaEntrada() {
return meiaEntrada;
}
public void setMeiaEntrada(int meiaEntrada) {
this.meiaEntrada = meiaEntrada;
}
private int preco;
private int meiaEntrada;
@FXML
private void initialize() {
}
public void setDialogStage(Stage dialogStage) {
this.dialogStage = dialogStage;
}
public void VenderIngresso(Filmes filme) {
this.filme = filme;
tituloLabel.setText(filme.getNome());
salaLabel.setText(filme.getSalaNumero());
categoriaLabel.setText(filme.getCategoria());
diretorLabel.setText(filme.getDiretor());
duracaoLabel.setText(filme.getDuracao());
protagonistaLabel.setText(filme.getProtagonista());
classificacaoLabel.setText(filme.getClassificacao());
if (filme.get3D() == true) {
e3dLabel.setText("Sim");
} else {
e3dLabel.setText("Não");
}
}
public void setCategoria (String nome) {
categoriaLabel.setText(nome);
}
@FXML
void handleCancelarIngresso(ActionEvent event) {
dialogStage.close();
}
@FXML
void handleGerarIngresso(ActionEvent event) {
}
}
So what I tried to do was the following: venderIngressoController.setCategoria(filmeSelecionado.getCategoria());
However, this returns me a NullPointer, does anyone know what I can do to fix it? I tried the American stackoverflow and could not find a similar question with 2 controllers.
Edit: Stacktrace
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 52 more
Caused by: java.lang.NullPointerException
at projeto.resources.EditarFilmeController.setFilme(EditarFilmeController.java:65)
at projeto.MainApp.showEditarFilmeDialog(MainApp.java:105)
at projeto.resources.FilmeOverviewController.handleEditarFilme(FilmeOverviewController.java:145)
... 62 more
The same way I put it up there, I tried to use capacidadeField.setText(Integer.toString(filme.getCapacidade()));
in another class and I had the same problem, however, I can not figure out since I've initialized the variable