Pane recovered from fxml file come null

Asked

Viewed 88 times

0

I am trying to recover a fxml file Pane for a controller, for the purpose of adding actions to its components. I am using the @FXML notation to bring this panel to the class, but it comes with null value.

Model controller:

package views.controllers;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.Pane;

public abstract class ControllerView implements Initializable {

@FXML
protected Pane painelInicial;

protected Pane painelAtual = painelInicial;

public void alternarPainel(Pane novoPainel){
    if(painelAtual != null){
        painelAtual.setVisible(false);
    }

    painelAtual = novoPainel;
    painelAtual.setVisible(true);
}

}

Controller that handles the panel that calls the null panel:

package views.controllers;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;

public class ControllerPainelOpcoesVenda extends ControllerView {

@FXML
private Button botaoCadastrarVenda;

@FXML
private Pane painelCadastroVenda;

@Override
public void initialize(URL location, ResourceBundle resources) {

    botaoCadastrarVenda.setOnAction(event->{
        alternarPainel(painelCadastroVenda);
    });
}
}

FXML file from null panel:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.chart.*?>
<?import javafx.scene.shape.*?>
<?import java.net.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.paint.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<Pane fx:id="painelCadastroVenda" layoutY="100.0" prefHeight="480.0" prefWidth="850.0" styleClass="painel" visible="false" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <Label fx:id="labelTituloCadastroVenda" layoutX="248.0" layoutY="24.0" styleClass="titulo" text="CADASTRO DE VENDA" />
        <TableView fx:id="tabelaListaProdutos" layoutX="30.0" layoutY="90.0" prefHeight="280.0" prefWidth="588.0">
            <columns>
                <TableColumn fx:id="colunaProduto" prefWidth="312.0" text="PRODUTO" />
                <TableColumn fx:id="colunaQuantidade" prefWidth="118.0" text="QUANTIDADE" />
                <TableColumn fx:id="colunaPrecoTotal" prefWidth="177.0" text="PREÇO TOTAL" />
            </columns>
        </TableView>
        <Button fx:id="botaoAdicionarProduto" layoutX="680.0" layoutY="128.0" mnemonicParsing="false" prefHeight="71.0" prefWidth="117.0" styleClass="botoes-padrao" text="ADICIONAR PRODUTO" wrapText="true" />
        <Pane fx:id="painelBotoesFormularioVenda" layoutY="429.0" prefHeight="53.0" prefWidth="850.0" style="-fx-background-color: gray;">
            <children>
                <Button fx:id="botaoSalvarCadastroProduto1" layoutX="686.0" layoutY="12.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="150.0" styleClass="botoes-padrao" text="SALVAR" />
                <Button fx:id="botaoVoltarCadastroProduto1" layoutX="14.0" layoutY="12.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="100.0" styleClass="botoes-padrao" text="VOLTAR" />
            </children>
        </Pane>
        <Button fx:id="botaoRemoverProduto" layoutX="680.0" layoutY="272.0" mnemonicParsing="false" prefHeight="71.0" prefWidth="117.0" styleClass="botoes-padrao" text="REMOVER PRODUTO" wrapText="true" />
        <Label fx:id="labelTotalVenda" layoutX="437.0" layoutY="391.0" style="-fx-font-size: 20px; -fx-text-fill: gray;" text="TOTAL" />
        <Label fx:id="labelValorTotalVenda" layoutX="541.0" layoutY="391.0" style="-fx-font-size: 20px; -fx-text-fill: #4caf50;" text="0,00" />
    </children>
</Pane>

Panel that the controller handles:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.chart.*?>
<?import javafx.scene.shape.*?>
<?import java.net.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.paint.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<Pane fx:id="painelOpcoesVenda" layoutY="100.0" maxHeight="480.0" maxWidth="850.0" minHeight="480.0" minWidth="850.0" prefHeight="480.0" prefWidth="850.0" styleClass="painel" visible="false" fx:controller="views.controllers.ControllerPainelOpcoesVenda" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <Button fx:id="botaoCadastrarVenda" layoutX="230.0" layoutY="72.0" mnemonicParsing="false" prefWidth="117.0" styleClass="botoes-crud" text="NOVA VENDA" wrapText="true" />
        <Button fx:id="botaoRemoverVenda" layoutX="507.0" layoutY="72.0" mnemonicParsing="false" prefWidth="117.0" styleClass="botoes-crud" text="REMOVER VENDA" wrapText="true" />
        <Button fx:id="botaoAtualizarVenda" layoutX="230.0" layoutY="288.0" mnemonicParsing="false" prefWidth="117.0" styleClass="botoes-crud" text="ATUALIZAR VENDA" wrapText="true" />
        <Button fx:id="botaoVisualizarVenda" layoutX="507.0" layoutY="288.0" mnemonicParsing="false" prefWidth="117.0" styleClass="botoes-crud" text="VISUALIZAR VENDA" wrapText="true" />
    </children>
</Pane>

Main panel FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.chart.*?>
<?import javafx.scene.shape.*?>
<?import java.net.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.paint.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<Pane fx:id="janelaPrincipal" maxHeight="580.0" maxWidth="850.0" minHeight="580.0" minWidth="850.0" prefHeight="580.0" prefWidth="850.0" styleClass="painel" stylesheets="@../css/estilo.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="views.controllers.ControllerPainelMenu">
   <children>
      <Pane fx:id="painelMenu" maxHeight="100.0" maxWidth="850.0" minHeight="100.0" minWidth="850.0" prefHeight="100.0" prefWidth="850.0" style="-fx-background-color: gray;">
         <children>
            <Button fx:id="botaoClientes" layoutX="14.0" layoutY="13.0" mnemonicParsing="false" prefHeight="71.0" prefWidth="224.0" styleClass="botoes-padrao" text="CLIENTES" textAlignment="CENTER" textFill="#0a0d0b7d" />
            <Button fx:id="botaoProdutos" layoutX="313.0" layoutY="13.0" mnemonicParsing="false" prefHeight="71.0" prefWidth="224.0" styleClass="botoes-padrao" text="PRODUTOS" textAlignment="CENTER" textFill="#0d0101" />
            <Button fx:id="botaoVendas" layoutX="612.0" layoutY="13.0" mnemonicParsing="false" prefHeight="71.0" prefWidth="224.0" styleClass="botoes-padrao" text="VENDAS" textAlignment="CENTER" textFill="#170202" />
         </children>
      </Pane>
      <Pane fx:id="painelInicial" layoutY="100.0" prefHeight="480.0" prefWidth="850.0" style="-fx-background-color: gray;" styleClass="painel">
         <children>
            <PieChart fx:id="graficoDeEstatisticas" layoutX="175.0" layoutY="40.0" title="ESTATISTICAS" />
         </children></Pane>
      <fx:include fx:id="painelOpcoesCliente" source="PainelOpcoesCliente.fxml" />
      <fx:include fx:id="painelOpcoesProduto" source="PainelOpcoesProduto.fxml" />
      <fx:include fx:id="painelOpcoesVenda" source="PainelOpcoesVenda.fxml" />
      <fx:include fx:id="painelCadastroCliente" source="PainelCadastroCliente.fxml" /> 
      <fx:include fx:id="painelCadastroProduto" source="PainelCadastroProduto.fxml" />
      <fx:include fx:id="painelCadastroVenda" source="painelCadastroVenda.fxml" />      
   </children>  
</Pane>

Main:

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Main extends Application {

private Stage palco;
private Pane painelPrincipal;

@Override
public void start(Stage primaryStage) throws Exception {

    this.palco = primaryStage;
    iniciarJanelaPrincipal();       
}

private void iniciarJanelaPrincipal() {
    try {

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("views/fxml/JanelaPrincipal.fxml"));
        painelPrincipal = loader.load();
        Scene scene = new Scene(painelPrincipal);
        palco.setScene(scene);
        palco.setResizable(false);
        palco.show();
    } 
    catch (IOException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args){
    launch();
}
}

Error that happens when I click on the button to register:

Exception in thread "Javafx Application Thread" java.lang.Nullpointerexception at views.controllers.Controllerview.alternarPainel(Controllerview.java:20) at views.controllers.Controllerpainelopcoescliente.lambda$0(Controllerpainelopcoescliente.java:31) 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.Eventutil.fireEventImpl(Eventutil.java:74) at com.sun.javafx.event.Eventutil.fireEvent(Eventutil.java:49) javafx.event.Event.fireEvent(Event.java:198) 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.Eventutil.fireEventImpl(Eventutil.java:74) at com.sun.javafx.event.Eventutil.fireEvent(Eventutil.java:54) javafx.event.Event.fireEvent(Event.java:198) javafx.scene.Scene$Mousehandler.process(Scene.java:3757) at javafx.scene.Scene$Mousehandler.access$1500(Scene.java:3485) 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:381) 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$354(Glassvieweventhandler.java:417) at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Quantumtoolkit.java:389) at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Glassvieweventhandler.java:416) 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.gtk.GtkApplication. _runLoop(Native Method) at com.sun.Glass.ui.gtk.GtkApplication.lambda$null$49(Gtkapplication.java:139) at java.lang.Thread.run(Thread.java:745)

1 answer

1

Your null panel’s FXML does not have a controller, represented by the tag: fx:controller, so you must assign or create a controller for your Pane. Using your own code as an example, your dashboard controller should look like this:

fx:controller="views.controllers.ControllerPainelMenu

Browser other questions tagged

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