É o seguinte…
Estou estudando o desenvolvimento em JavaFX para desktops e me deparei com o seguinte o problema:
Eu tenho um menu principal, ou seja, um FXML com um Border Pane que carrega dois outros FXMLs (sendo eles a barra de título [AnchorPane], por assim dizer, e o menu lateral [JFXDrawer]).
No controlador do Menu Principal eu tenho alguns métodos que devem ser acessados, em algum momento, pelos controladores dos outros FXMLs. Portanto, no método Initialize() do Menu Principal, eu chamo os métodos que carregam os dois outros FXMLs, passando como parâmetro o controlador do Menu Principal e gravando esse parâmetro numa instância do controlador do menu principal criada nesses controladores “secundários”.
No entanto, no controlador do menu lateral, ao clicar no botão Home, que chama o método changeDrawer(), do menu principal, é retornada uma NullPointerException. Acredito que seja porque, por algum motivo, nada esteja sendo gravado nessa instância, ou o controlador não esteja sendo passado corretamente como parâmetro…
Os dois controladores a seguir para melhor entendimento (não sei se fui muito claro rsrs):
CONTROLADOR DO MENU PRINCIPAL
package Controller;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.BorderPane;
public class MenuPrincipalController {
@FXML BorderPane rootStage;//The rootStage of the app.
//Controllers that will use the MenuPrincipalController.
@FXML MenuPrincipal_BarraSuperiorController topMenuController;
@FXML MenuPrincipal_GavetaMenusController sideDrawerController;
//Initializers.
@FXML
public void initialize() {
//Initialize the Main Screen Active Controllers.
topMenuController.init(this);
sideDrawerController.init(this);
//Resets hierarchy at the start of the application.
//updateHierarchy("3");
rootStage.requestFocus();
}
//Set a specific frame on the burgerAnimation and play it.
public void setBurgerAnimation(Double value) {
topMenuController.getBurgerAnimation().setRate(value);
topMenuController.getBurgerAnimation().play();
}
//Method to display/hide the drawerMenu and it's SubMenus.
public void changeDrawer() {
//Show/Hide the Drawer state.
sideDrawerController.getDrawer().toggle();
//Hide the drawer's sideMenus.
sideDrawerController.changeSubDrawerState(null);
//Get the animation stage of the hamburger.
//If it didn't started then will start and show the drawerMenu.
if (topMenuController.getBurgerAnimation().getRate() == -1) {
topMenuController.doAnimation();
} else {
//If not then it will restart.
topMenuController.doAnimation();
}
}
//Method to control the hierarchy at each Login/Logoff successful attempt.
public void updateHierarchy(String hierarchy) {
sideDrawerController.hierarchyControl(hierarchy);
}
// Função para mostrar um alert (INFORMATION, ERROR, WARNING), ou seja, somente uma mensagem
public void Mostrar_Alert(AlertType Tipo, String Titulo, String Cabecalho, String Conteudo) {
Alert msgDialogo = new Alert(Tipo);
msgDialogo.setTitle(Titulo);
msgDialogo.setHeaderText(Cabecalho);
msgDialogo.setContentText(Conteudo);
msgDialogo.showAndWait();
}
}
CONTROLADOR DO MENU LATERAL
package Controller;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDrawer;
import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIcon;
import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
public class MenuPrincipal_GavetaMenusController {
//MainScreenController Instance.
private MenuPrincipalController main;
@FXML private JFXDrawer drawer;
@FXML private AnchorPane anchorPane;
@FXML private JFXButton btnHome;
@FXML private JFXButton btnCadastros;
@FXML private JFXButton btnServicos;
@FXML private JFXDrawer drawerCadastros;
@FXML private JFXDrawer drawerServicos;
@FXML void btnCadastrosMouseClicked(ActionEvent event) {
changeSubDrawerState(drawerCadastros);
}
@FXML void btnHomeMouseClicked(ActionEvent event) {
main.changeDrawer();
}
@FXML void btnServicosMouseClicked(ActionEvent event) {
changeSubDrawerState(drawerServicos);
}
@FXML void drawerClickReleased(MouseEvent event) {
drawer.close();
if (!drawer.isVisible()) {
//Plays the "close" burgerAnimation.
main.setBurgerAnimation(-1.0);
}
}
//Initializer.
public void init(MenuPrincipalController main) {
this.main = main;
//Initializes the drawerMenus
//Set the SideMenu on the sidePane of the drawerMenu.
drawer.setSidePane(anchorPane);
//Call the method to create the GridPanes for the sub-menus.
drawerCadastros.setSidePane(initMenuCadastros());
drawerServicos.setSidePane(initMenuServicos());
}
// SUBMENU'S STATE CHANGE METHOD.
// Show/Hide Drawers based on the param.
public void changeSubDrawerState(JFXDrawer drawer) {
// If the drawer already is shown, close it.
if (drawerCadastros.isVisible()
|| drawerCadastros.isVisible()
|| !drawerCadastros.equals(drawer)) {
drawerCadastros.close();
drawerCadastros.toBack();
drawerCadastros.setVisible(false);
// btnServicos gets back to it's original position.
btnServicos.setTranslateY(0);
// If it isn't.
} else {
// btnServicos goes down to display the drawerCadastros SubMenu.
btnServicos.setTranslateY(162);
drawerCadastros.setVisible(true);
drawerCadastros.toFront();
drawerCadastros.open();
}
// DrawerServicos Variant.
// Here the btnServicos position doesn't need to be changed.
if (drawerServicos.isVisible()
|| drawerServicos.isVisible()
|| !drawerServicos.equals(drawer)) {
drawerServicos.close();
drawerServicos.toBack();
drawerServicos.setVisible(false);
} else {
drawerServicos.setVisible(true);
drawerServicos.toFront();
drawerServicos.open();
}
}
// SUB-MENU EVENTHANDLER METHOD.
// Cadastros
public GridPane initMenuCadastros() {
//Creates the Buttons.
JFXButton btnCadastros_Clientes = new JFXButton("Clientes");
JFXButton btnCadastros_Cuidadores = new JFXButton("Cuidadores");
JFXButton btnCadastros_Pets = new JFXButton("Pets");
//Handler to show the Cadastros_Clientes screen.
btnCadastros_Clientes.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
//Method to call the scene.
//main.showUsuarioAdd();
//Method to hide the drawer.
//main.changeDrawer();
});
//Handler to show the Cadastros_Cuidadores screen.
btnCadastros_Cuidadores.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
//Method to call the scene.
//main.showUsuarioSearch();
//Method to hide the drawer.
//main.changeDrawer();
});
//Handler to show the Cadastros_Pets screen.
btnCadastros_Pets.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
//Method to call the scene.
//main.showUsuarioSearch();
//Method to hide the drawer.
//main.changeDrawer();
});
//Creates the Button Icons.
MaterialDesignIconView iconCli = new MaterialDesignIconView(
MaterialDesignIcon.ACCOUNT_PLUS);
MaterialDesignIconView iconCui = new MaterialDesignIconView(
MaterialDesignIcon.ACCOUNT_SWITCH);
MaterialDesignIconView iconPet = new MaterialDesignIconView(
MaterialDesignIcon.BONE);
iconCli.setSize("15.0");
iconCui.setSize("15.0");
iconPet.setSize("15.0");
// Adjust the button settings.
// Clientes
btnCadastros_Clientes.setPrefWidth(217);
btnCadastros_Clientes.setPrefHeight(54);
btnCadastros_Clientes.setGraphic(iconCli);
btnCadastros_Clientes.setGraphicTextGap(20);
btnCadastros_Clientes.setPadding(new Insets(0, 0, 0, 50));
btnCadastros_Clientes.setAlignment(Pos.CENTER_LEFT);
// Cuidadores
btnCadastros_Cuidadores.setPrefWidth(217);
btnCadastros_Cuidadores.setPrefHeight(54);
btnCadastros_Cuidadores.setGraphic(iconCui);
btnCadastros_Cuidadores.setGraphicTextGap(20);
btnCadastros_Cuidadores.setPadding(new Insets(0, 0, 0, 50));
btnCadastros_Cuidadores.setAlignment(Pos.CENTER_LEFT);
// Pets
btnCadastros_Pets.setPrefWidth(217);
btnCadastros_Pets.setPrefHeight(54);
btnCadastros_Pets.setGraphic(iconPet);
btnCadastros_Pets.setGraphicTextGap(20);
btnCadastros_Pets.setPadding(new Insets(0, 0, 0, 50));
btnCadastros_Pets.setAlignment(Pos.CENTER_LEFT);
//Create grid and add the buttons to it.
GridPane grid = new GridPane();
grid.setVgap(0);
grid.setPadding(Insets.EMPTY);
grid.add(btnCadastros_Clientes, 0, 0);
grid.add(btnCadastros_Cuidadores, 0, 1);
grid.add(btnCadastros_Pets, 0, 2);
//Return the grid.
return grid;
}
//Method to control the hierarchy within the program.
public void hierarchyControl(String hierarchy) {
switch (hierarchy) {
case "1":
btnCadastros.setDisable(true);
btnServicos.setDisable(true);
break;
case "2":
btnCadastros.setDisable(true);
btnServicos.setDisable(false);
break;
case "3":
btnCadastros.setDisable(false);
btnServicos.setDisable(false);
break;
}
}
//Getter for the drawerMenu.
public JFXDrawer getDrawer() {
return drawer;
}
// Função para mostrar um alert (INFORMATION, ERROR, WARNING), ou seja, somente uma mensagem
public void Mostrar_Alert(AlertType Tipo, String Titulo, String Cabecalho, String Conteudo) {
Alert msgDialogo = new Alert(Tipo);
msgDialogo.setTitle(Titulo);
msgDialogo.setHeaderText(Cabecalho);
msgDialogo.setContentText(Conteudo);
msgDialogo.showAndWait();
}
}
ERRO QUE DÁ AO CLICAR NO BOTÃO HOME
Executing C:\Users\Diego\Documents\NetBeansProjects\Dogs_FX\dist\run327492304\Dogs_FX.jar using platform C:\Program Files\Java\jdk1.8.0_191\jre/bin/java
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:8411)
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:1769)
… 52 more
Caused by: java.lang.NullPointerException
at Controller.MenuPrincipal_GavetaMenusController.btnHomeMouseClicked(MenuPrincipal_GavetaMenusController.java:38)
… 62 more