CommandButton does not register in SQL

0

I'm having trouble with my code, when I make a record of a new vehicle, it says that the vehicle is not being instantiated and is returning null. Here are my settings:

DAOLayer:

publicvoidadicionar(Veiculoveiculo)throwsSQLException{StringBuildersql=newStringBuilder();sql.append("INSERT INTO VEICULO(placa, tipo, fabricante, modelo, empresa_origem, veiculo_ceped, descricao) ");
    sql.append("VALUE(?, ?, ?, ?, ?, ?, ?)");

    Connection conexao = ConnectionFactory.conectar();

    PreparedStatement comando = conexao.prepareStatement(sql.toString());
    comando.setString(1, veiculo.getPlaca());
    comando.setString(2, veiculo.getTipo());
    comando.setString(3, veiculo.getFabricante());
    comando.setString(4, veiculo.getModelo());
    comando.setString(5, veiculo.getEmpresa_origem());
    comando.setBoolean(6, veiculo.getVeiculo_ceped());
    comando.setString(7, veiculo.getDescricao());

    comando.executeUpdate();
}

BEAN layer:

@ManagedBean(name="VeiculoMB")
@ViewScoped
public class VeiculoBEAN {

    private ListDataModel<Veiculo> veiculos;

    private Veiculo veiculo;

    public ListDataModel<Veiculo> getVeiculos() {
        return veiculos;
    }

    public void setVeiculos(ListDataModel<Veiculo> veiculos) {
        this.veiculos = veiculos;
    }

    public void prepararNovo() {
        veiculo = new Veiculo();
    }

    public Veiculo getVeiculo() {
        return veiculo;
    }

    public void setVeiculo(Veiculo veiculo) {
        this.veiculo = veiculo;
    }

    public void prepararVeiculo() {
        veiculo = veiculos.getRowData();
    }

    public void veiculoNovo() {
        VeiculoDAO veiDAO = new VeiculoDAO();
        try {
            veiDAO.adicionar(veiculo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

XHTML:

   <?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    template="/templates/menu_padrao.xhtml">

    <ui:define name="menu">
        <ui:include src="/includes/menu_bar.xhtml" />
    </ui:define>

    <ui:define name="conteudo">
        <f:facet name="header">
            <h:outputText value="Funcionario - Entrada"></h:outputText>
        </f:facet>

        <h:form id="frm_funcEntrada">

            <h:panelGrid id="panel_visitaNova" columns="4">
                <h:outputText value="Funcionario: "></h:outputText>
                <p:inputMask mask="9" />

                <h:outputText value="Data: "></h:outputText>
                <p:calendar id="mask" pattern="dd-MM-yyyy" mask="true" />

                <h:outputText value="Entrada: "></h:outputText>
                <p:inputMask id="hora" size="5" mask="99:99" />

                <p:outputLabel value="Placa:" />
                <p:inputMask id="placa" mask="9" />

                <p:commandButton value="Motorizado?" icon=" ui-icon-clipboard"
                    action="#{veiculoMB.prepararNovo()}"
                    oncomplete="PF('dialog_Motorizado').show();" />

                <p:commandButton value="Registrar Entrada" icon="ui-icon-plus"></p:commandButton>

            </h:panelGrid>

<form id="formDailog">


        <p:dialog widgetVar="dialog_Motorizado" draggable="false"
            resizable="false" appendTo="@(body)" modal="true"
            header="Informações Veículo">


                <h:panelGrid columns="4">

                    <h:outputText value="Placa: "></h:outputText>
                    <p:inputMask id="placa" size="9" value="#{veiculoMB.veiculo.placa}"
                        mask="aaa-9999" />

                    <h:outputText value="Tipo: "></h:outputText>
                    <p:selectOneMenu id="tipo" value="#{veiculoMB.veiculo.tipo}"
                        style="width:125px">
                        <f:selectItem itemLabel="Escolha..." itemValue="" />
                        <f:selectItem itemLabel="Carro" itemValue="Carro" />
                        <f:selectItem itemLabel="Moto" itemValue="Moto" />
                        <f:selectItem itemLabel="Caminhão" itemValue="Caminhao" />
                    </p:selectOneMenu>

                    <h:outputText value="Fabricante: "></h:outputText>
                    <p:selectOneMenu id="fabricante"
                        value="#{veiculoMB.veiculo.fabricante}" style="width:125px">
                        <f:selectItem itemLabel="Escolha..." itemValue="" />
                        <f:selectItem itemLabel="Fiat" itemValue="Fiat" />
                        <f:selectItem itemLabel="Renault" itemValue="Renault" />
                        <f:selectItem itemLabel="Nissan" itemValue="Nissan" />
                    </p:selectOneMenu>

                    <h:outputText value="Modelo: "></h:outputText>
                    <p:inputText size="10" id="modelo"
                        value="#{veiculoMB.veiculo.modelo}" />

                    <h:outputText value="Empresa: "></h:outputText>
                    <p:inputText size="25" id="empresa_origem"
                        value="#{veiculoMB.veiculo.empresa_origem}" />

                    <h:outputText value="Veículo CEPED: " />
                    <p:selectOneButton id="veiculo_ceped"
                        value="#{veiculoMB.veiculo.veiculo_ceped}">
                        <f:selectItem itemLabel="Sim" itemValue="1" />
                        <f:selectItem itemLabel="Não" itemValue="0" />
                    </p:selectOneButton>

                    <h:outputText value="Descrição: "></h:outputText>
                    <p:inputText size="25" id="descricao"
                        value="#{veiculoMB.veiculo.descricao}" />


                </h:panelGrid>

                <p:commandButton value="Registrar Veículo" icon="ui-icon-plus"
                    action="#{veiculoMB.veiculoNovo()}"
                    oncomplete="PF('dialog_Motorizado').hide();" />

            </form>
    </h:form>
        </p:dialog>
    </ui:define>

</ui:composition>

ERROR Image:

    
asked by anonymous 01.02.2018 / 18:36

1 answer

0

Your objeto Vehicle is null: Starts it on your VeiculoMB .

    @ManagedBean(name="veiculoMB")//caixa baixa
    @ViewScoped 
    public class VeiculoBEAN {

    private Veiculo veiculo = new Veiculo();

    ///implementação dos metodos...



    public void veiculoNovo() {

        VeiculoDAO veiDAO = new VeiculoDAO();
        try {
            veiDAO.adicionar(this.veiculo);
        } catch (Exception e) {
            e.printStackTrace();
        }

    // getts e setts veiculo

    }

Also see the name of VeiculoMB seems to be wrong see {VeiculoMB.veiculo.tipo} I think correct is {veiculoMB.veiculo.tipo} the first letter has to be in lowercase.

@ManagedBean(name="veiculoMB")//caixa baixa
@ViewScoped 
public class VeiculoBEAN {

private ListDataModel<Veiculo> veiculos;

private Veiculo veiculo = new Veiculo();// inicia o seu objeto.

public ListDataModel<Veiculo> getVeiculos() {
    return veiculos;
}

public void setVeiculos(ListDataModel<Veiculo> veiculos) {
    this.veiculos = veiculos;
}

public void prepararNovo() {
    veiculo = new Veiculo();
}

public Veiculo getVeiculo() {
    return veiculo;
}

public void setVeiculo(Veiculo veiculo) {
    this.veiculo = veiculo;
}

public void prepararVeiculo() {
    veiculo = veiculos.getRowData();
}

public void veiculoNovo() {
    VeiculoDAO veiDAO = new VeiculoDAO();
    try {
        veiDAO.adicionar(veiculo);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

.xhtml:

<p:commandButton value="Registrar Veículo" icon="ui-icon-plus"
                actionListener="#{veiculoMB.veiculoNovo()}"
                oncomplete="PF('dialog_Motorizado').hide();" ajax="false" />

or

<p:commandButton value="Registrar Veículo" icon="ui-icon-plus"
                action="#{veiculoMB.veiculoNovo()}"
                oncomplete="PF('dialog_Motorizado').hide();" />

If you want to use Ajax:

<p:commandButton value="Registrar Veículo" icon="ui-icon-plus"
                    oncomplete="PF('dialog_Motorizado').hide();" >
 <p:ajax   listener="#{veiculoMB.veiculoNovo()}" />
</p:commandButton>
    
01.02.2018 / 18:48