Tenho uma aplicação onde preciso calcular dados vindos de um determinado relacionamento.
Quando usuarioController.view == 3
, define-se um Login
para Usuario
e um Plano
(já persistido) para Login
para que na próxima etapa o UsuarioController
calcule a soma do preço de todos os planos contido em todos os logins, porém o Login
não está populado com o Plano
.
Será esse um problema no Converter?
Usuario
@Entity
@XmlRootElement
@Table(uniqueConstraints = @UniqueConstraint(columnNames = "rg"))
public class Usuario implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@NotNull
@Size(min = 1, max = 150)
@Pattern(regexp = "[^0-9]*", message = "Não deve conter números")
private String nome;
@NotNull
private String rg;
private String cpf;
private String cnpj;
@Temporal(TemporalType.DATE)
private Date dataNascimento;
@OneToOne(cascade = CascadeType.ALL)
private Contato contato;
@OneToOne(cascade = CascadeType.ALL)
private Endereco endereco;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "usuario_id")
private List<Login> login = new ArrayList<Login>();
@Enumerated
private Pessoa pessoa;
@Enumerated
private TipoCadastro tipoCadastro;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "usuario_id")
private List<Cobranca> cobranca;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getRg() {
return rg;
}
public void setRg(String rg) {
this.rg = rg;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getCnpj() {
return cnpj;
}
public void setCnpj(String cnpj) {
this.cnpj = cnpj;
}
public Contato getContato() {
return contato;
}
public void setContato(Contato contato) {
this.contato = contato;
}
public Endereco getEndereco() {
return endereco;
}
public void setEndereco(Endereco endereco) {
this.endereco = endereco;
}
public List<Login> getLogin() {
return login;
}
public void setLogin(List<Login> login) {
this.login = login;
}
public void addLogin(Login login){
this.login.add(login);
System.out.println("Numero de logins: " + getLogin().size());
}
public void addLogin(){
addLogin(new Login());
}
public Pessoa getPessoa() {
return pessoa;
}
public void setPessoa(Pessoa pessoa) {
this.pessoa = pessoa;
}
public TipoCadastro getTipoCadastro() {
return tipoCadastro;
}
public void setTipoCadastro(TipoCadastro tipoCadastro) {
this.tipoCadastro = tipoCadastro;
}
public Date getDataNascimento() {
return dataNascimento;
}
public void setDataNascimento(Date dataNascimento) {
this.dataNascimento = dataNascimento;
}
public List<Cobranca> getCobranca() {
return cobranca;
}
public void setCobranca(List<Cobranca> cobranca) {
this.cobranca = cobranca;
}
public void addCobranca(Cobranca cobranca){
if(this.cobranca == null)
this.cobranca = new ArrayList<Cobranca>();
this.cobranca.add(cobranca);
}
public void addCobranca(){
addCobranca(new Cobranca());
}
Login
@Entity
@XmlRootElement
@Dependent
public class Login implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@ManyToOne
private Usuario usuario;
@ManyToOne(cascade = CascadeType.ALL)
private Plano plano;
private String user;
private String pass;
private String mac;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Usuario getUsuario() {
return usuario;
}
public void setUsuario(Usuario usuario) {
this.usuario = usuario;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public Plano getPlano() {
return plano;
}
public void setPlano(Plano plano) {
System.out.println("Setando plano " + plano.getNome());
this.plano = plano;
}
public String getMac() {
return mac;
}
public void setMac(String mac) {
this.mac = mac;
}
@Override
public String toString() {
return "Login [id=" + id + ", usuario=" + usuario + ", plano=" + plano
+ ", user=" + user + ", pass=" + pass + "]";
}
}
UsuarioController
@Model
@SessionScoped
public class UsuarioController implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Inject
private FacesContext facesContext;
@Inject
private UsuarioRegistration usuarioRegistration;
@Produces
@Named
private Usuario novoUsuario;
private Integer view = 1;
@PostConstruct
public void initNovoUsuario(){
novoUsuario = new Usuario();
novoUsuario.setEndereco(new Endereco());
novoUsuario.setContato(new Contato());
}
public void register() throws Exception{
try{
usuarioRegistration.register(novoUsuario);
FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_INFO, "Registred!", "Registration successful");
facesContext.addMessage(null, m);
initNovoUsuario();
} catch (Exception e){
String errorMessage = getRootErrorMessage(e);
FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, "Registration unsuccessful");
facesContext.addMessage(null, m);
}
}
private String getRootErrorMessage(Exception e){
String errorMessage = "Registration falied. See server log for more information";
if (e == null)
return errorMessage;
Throwable t = e;
while (t != null){
errorMessage = t.getLocalizedMessage();
t = t.getCause();
}
return errorMessage;
}
public List<Pessoa> getPessoa(){
return Arrays.asList(Pessoa.values());
}
public List<UF> getUf(){
return Arrays.asList(UF.values());
}
public List<TipoCadastro> getTipoCadastro(){
return Arrays.asList(TipoCadastro.values());
}
public Integer getView() {
return view;
}
public void setView(Integer view) {
this.view = view;
}
public void proximoView(){
view++;
}
public void anteriorView(){
view--;
}
public void removeLogin(Login login){
if(novoUsuario.getLogin().contains(login))
novoUsuario.getLogin().remove(login);
}
public void removeCobranca(Cobranca cobranca){
if(novoUsuario.getCobranca().contains(cobranca))
novoUsuario.getCobranca().remove(cobranca);
}
public void addCobranca(){
Double d = 0.0;
for(Login l : novoUsuario.getLogin())
d += l.getPlano().getValor();
novoUsuario.addCobranca(new Cobranca("Mensalidade" , d));
}
PlanoConverter
@FacesConverter(value = "planoConverter", forClass = Plano.class)
@Named
public class PlanoConverter implements Converter {
@Override
public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
if (string != null && !string.isEmpty()) {
return (Plano) uic.getAttributes().get(string);
}
return null;
}
@Override
public String getAsString(FacesContext fc, UIComponent uic, Object o) {
if (o != null && (o instanceof Plano)) {
return String.valueOf(((Plano) o).getId());
}
return null;
}
}
novoUsuario.xhtml
<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:ui="http://java.sun.com/jsf/facelets"
xmlns:b="http://bootsfaces.net/ui"
xmlns:p="http://primefaces.org/ui" template="WEB-INF/templates/default/main.xhtml">
<ui:define name="content">
<h1 class="page-header "> <i class="fa fa-tachometer"></i> Cliente</h1>
<ol class="breadcrumb">
<li><a href="#">Cliente</a></li>
<li class="active">Novo Cliente</li>
</ol>
<h:form >
<b:panel rendered="#{usuarioController.view == 1}">
<h2>Cadastro de Clientes</h2>
<p>Enforces annotation-based constraints defined on the
model class.
</p>
<b:messages styleClass="messages"
errorClass="invalid" infoClass="valid"
warnClass="warning" globalOnly="true"/>
<h:panelGrid columns="3" columnClasses="titleCell">
<h:outputLabel for="nome" value="Tipo de Cadastro: " />
<b:selectOneMenu id="pessoa" value="#{novoUsuario.pessoa}">
<f:selectItems value="#{usuarioController.pessoa}" var="pessoa" itemLabel="#{pessoa.descricao}" itemValue="#{pessoa}"/>
</b:selectOneMenu>
<h:message for="nome" errorClass="invalid" />
</h:panelGrid>
<p>
<h:panelGrid columns="2">
</h:panelGrid>
</p>
</b:panel>
<b:panel rendered="#{usuarioController.view == 2}">
<h2>Cadastro de #{novoUsuario.pessoa.descricao}</h2>
<p>Enforces annotation-based constraints defined on the
model class.
</p>
<b:messages styleClass="messages"
errorClass="invalid" infoClass="valid"
warnClass="warning" globalOnly="true"/>
<h:panelGroup rendered="#{novoUsuario.pessoa eq 'FISICA'}">
<h:panelGrid columns="3" columnClasses="titleCell">
<h:outputLabel for="name" value="Name:" />
<b:inputText id="name" value="#{novoUsuario.nome}" />
<h:message for="name" errorClass="invalid" />
<h:outputLabel for="rg" value="Rg:" />
<b:inputText id="rg" value="#{novoUsuario.rg}" styleClass="rg" />
<h:message for="rg" errorClass="invalid" />
<h:outputLabel for="cpf" value="CPF:" />
<b:inputText id="cpf" value="#{novoUsuario.cpf}" />
<h:message for="cpf" errorClass="invalid" />
<h:outputLabel for="tel1" value="Telefone:" />
<b:inputText id="tel1" value="#{novoUsuario.contato.telefone}" />
<h:message for="tel1" errorClass="invalid" />
<h:outputLabel for="tel2" value="Telefone Trabalho:" />
<b:inputText id="tel2" value="#{novoUsuario.contato.telefoneD}" />
<h:message for="tel2" errorClass="invalid" />
<h:outputLabel for="celular" value="Celular:" />
<b:inputText id="celular" value="#{novoUsuario.contato.celular}" />
<h:message for="celular" errorClass="invalid" />
<h:outputLabel for="email" value="Email:" />
<b:inputText id="email" value="#{novoUsuario.contato.email}" />
<h:message for="email" errorClass="invalid" />
<h:outputLabel for="responsavel" value="Responsavel: " />
<b:inputText id="responsavel" value="#{novoUsuario.contato.responsavel}" />
<h:message for="responsavel" errorClass="invalid" />
<h:outputLabel for="uf" value="Estado: " />
<b:selectOneMenu id="uf" value="#{novoUsuario.endereco.uf}">
<f:selectItems value="#{usuarioController.uf}" var="uf" itemLabel="#{uf.descricao}" itemValue="#{uf}"/>
</b:selectOneMenu>
<h:message for="uf" errorClass="invalid" />
<h:outputLabel for="cidade" value="Cidade: " />
<b:inputText id="cidade" value="#{novoUsuario.endereco.cidade}" />
<h:message for="cidade" errorClass="invalid" />
<h:outputLabel for="cep" value="CEP: " />
<b:inputText id="cep" value="#{novoUsuario.endereco.cep}" />
<h:message for="cep" errorClass="invalid" />
<h:outputLabel for="bairro" value="Bairro: " />
<b:inputText id="bairro" value="#{novoUsuario.endereco.bairro}" />
<h:message for="bairro" errorClass="invalid" />
<h:outputLabel for="logadouro" value="Rua: " />
<b:inputText id="logadouro" value="#{novoUsuario.endereco.logadouro}" />
<h:message for="logadouro" errorClass="invalid" />
<h:outputLabel for="numero" value="Numero: " />
<b:inputText id="numero" value="#{novoUsuario.endereco.numero}" />
<h:message for="numero" errorClass="invalid" />
<h:outputLabel for="complemento" value="Complemento: " />
<b:inputText id="complemento" value="#{novoUsuario.endereco.complemento}" />
<h:message for="complemento" errorClass="invalid" />
<h:outputLabel for="tipoCadastro" value="Função: " />
<b:selectOneMenu id="tipoCadastro" value="#{novoUsuario.tipoCadastro}">
<f:selectItems value="#{usuarioController.tipoCadastro}" var="c" itemLabel="#{c.descricao}" itemValue="#{c}"/>
</b:selectOneMenu>
<h:message for="tipoCadastro" errorClass="invalid" />
</h:panelGrid>
</h:panelGroup>
</b:panel>
<b:panel rendered="#{usuarioController.view == 3}">
<h2>Internet</h2>
<p>Enforces annotation-based constraints defined on the
model class.
</p>
<b:messages styleClass="messages"
errorClass="invalid" infoClass="valid"
warnClass="warning" globalOnly="true"/>
<h:panelGrid columns="3" columnClasses="titleCell">
<h:dataTable id="tabelaLogin" value="#{novoUsuario.login}" var="l"
styleClass="table table-striped table-bordered">
<h:column>
<!-- column header -->
<f:facet name="header">Usuário</f:facet>
<!-- row record -->
<b:inputText id="user" value="#{l.user}" />
</h:column>
<h:column>
<f:facet name="header">Senha</f:facet>
<b:inputText id="pass" value="#{l.pass}" />
</h:column>
<h:column>
<f:facet name="header">MAC</f:facet>
<b:inputText id="mac" value="#{l.mac}" />
</h:column>
<h:column>
<f:facet name="header">Plano</f:facet>
<p:selectOneMenu id="plano" value="#{l.plano}" converter="planoConverter">
<f:selectItems value="#{planoController.findAll()}" var="plano" itemLabel="#{plano.nome}" itemValue="#{plano}"/>
</p:selectOneMenu>
</h:column>
<h:column>
<f:facet name="header">#</f:facet>
<b:commandButton action="#{usuarioController.removeLogin(l)}" value="Apagar" look="danger" />
</h:column>
</h:dataTable>
</h:panelGrid>
</b:panel>
<b:panel rendered="#{usuarioController.view == 4}">
<h2>Cobrança</h2>
<p>Enforces annotation-based constraints defined on the
model class.
</p>
<b:messages styleClass="messages"
errorClass="invalid" infoClass="valid"
warnClass="warning" globalOnly="true"/>
<h:panelGrid columns="3" columnClasses="titleCell">
<h:dataTable id="tabelaCobranca" value="#{novoUsuario.cobranca}" var="f"
styleClass="table table-striped table-bordered">
<h:column>
<!-- column header -->
<f:facet name="header">ID</f:facet>
<!-- row record -->
<b:inputText id="id" value="#{f.id}" />
</h:column>
<h:column>
<f:facet name="header">Descrição</f:facet>
<b:inputText id="descricao" value="#{f.descricao}" />
</h:column>
<h:column>
<f:facet name="header">Valor</f:facet>
<b:inputText id="valor" value="#{f.valor}" />
</h:column>
<h:column>
<f:facet name="header">Vencimento</f:facet>
<b:inputText id="vencimento" value="#{f.vencimento}" />
</h:column>
<h:column>
<f:facet name="header">Enviar Cobrança</f:facet>
<h:selectOneRadio value="#{f.enviarCobranca}"></h:selectOneRadio>
</h:column>
<h:column >
<f:facet name="header">#</f:facet>
<b:commandButton action="#{usuarioController.removeCobranca(f)}" value="Apagar" look="danger" />
</h:column>
</h:dataTable>
</h:panelGrid>
</b:panel>
<b:commandButton id="anterior" action="#{usuarioController.anteriorView()}" value="Anterior" look="warning"/>
<b:commandButton value="Add Login" actionListener="#{novoUsuario.addLogin()}" update="tabelaLogin" look="success" rendered="#{usuarioController.view == 3}"/>
<b:commandButton value="Add Cobrança" actionListener="#{usuarioController.addCobranca()}" update="tabelaCobranca" look="success" rendered="#{usuarioController.view == 4}"/>
<b:commandButton id="proxima" action="#{usuarioController.proximoView()}" value="Próximo" look="primary"/>
</h:form>
Erro
1:38:07,985 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/AuthNET].[FacesServlet]] (http-localhost/127.0.0.1:8080-3) JBWEB000236: Servlet.service() for servlet FacesServlet threw exception: java.lang.NullPointerException
at com.authnet.model.Login.setPlano(Login.java:67) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_60]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_60]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_60]
at java.lang.reflect.Method.invoke(Method.java:497) [rt.jar:1.8.0_60]
at javax.el.BeanELResolver.setValue(BeanELResolver.java:383) [jboss-el-api_2.2_spec-1.0.4.Final-redhat-1.jar:1.0.4.Final-redhat-1]
at com.sun.faces.el.DemuxCompositeELResolver._setValue(DemuxCompositeELResolver.java:255) [jsf-impl-2.1.28.redhat-3.jar:2.1.28.redhat-3]
at com.sun.faces.el.DemuxCompositeELResolver.setValue(DemuxCompositeELResolver.java:281) [jsf-impl-2.1.28.redhat-3.jar:2.1.28.redhat-3]
at org.apache.el.parser.AstValue.setValue(AstValue.java:199) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.el.ValueExpressionImpl.setValue(ValueExpressionImpl.java:257) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.jboss.weld.el.WeldValueExpression.setValue(WeldValueExpression.java:64) [weld-core-1.1.23.Final-redhat-1.jar:1.1.23.Final-redhat-1]
at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:131) [jsf-impl-2.1.28.redhat-3.jar:2.1.28.redhat-3]
at javax.faces.component.UIInput.updateModel(UIInput.java:822) [jboss-jsf-api_2.1_spec-2.1.28.Final-redhat-1.jar:2.1.28.Final-redhat-1]
at javax.faces.component.UIInput.processUpdates(UIInput.java:739) [jboss-jsf-api_2.1_spec-2.1.28.Final-redhat-1.jar:2.1.28.Final-redhat-1]
at javax.faces.component.UIData.iterate(UIData.java:2013) [jboss-jsf-api_2.1_spec-2.1.28.Final-redhat-1.jar:2.1.28.Final-redhat-1]
at javax.faces.component.UIData.processUpdates(UIData.java:1266) [jboss-jsf-api_2.1_spec-2.1.28.Final-redhat-1.jar:2.1.28.Final-redhat-1]
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1244) [jboss-jsf-api_2.1_spec-2.1.28.Final-redhat-1.jar:2.1.28.Final-redhat-1]
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1244) [jboss-jsf-api_2.1_spec-2.1.28.Final-redhat-1.jar:2.1.28.Final-redhat-1]
at javax.faces.component.UIForm.processUpdates(UIForm.java:281) [jboss-jsf-api_2.1_spec-2.1.28.Final-redhat-1.jar:2.1.28.Final-redhat-1]
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1244) [jboss-jsf-api_2.1_spec-2.1.28.Final-redhat-1.jar:2.1.28.Final-redhat-1]
at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1244) [jboss-jsf-api_2.1_spec-2.1.28.Final-redhat-1.jar:2.1.28.Final-redhat-1]
at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:1223) [jboss-jsf-api_2.1_spec-2.1.28.Final-redhat-1.jar:2.1.28.Final-redhat-1]
at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:78) [jsf-impl-2.1.28.redhat-3.jar:2.1.28.redhat-3]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.28.redhat-3.jar:2.1.28.redhat-3]
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [jsf-impl-2.1.28.redhat-3.jar:2.1.28.redhat-3]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) [jboss-jsf-api_2.1_spec-2.1.28.Final-redhat-1.jar:2.1.28.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:231) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:926) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_60]