Nullpointexception problem in JSF and Primefaces

Asked

Viewed 26 times

1

Good afternoon! I am having problems with a xhtml form made in JSF, because it is not setting the values in Usuariobean, and when I will execute the method register() present in the class Usuariobean eclipse returns me an error of type Nullpointexception. Follow.xhtml and Usuariobean.class:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://xmlns.jcp.org/jsf/html"
	xmlns:p="http://primefaces.org/ui"
	xmlns:f="http://xmlns.jcp.org/jsf/core">


<h:form id="cadastro">
	<p:growl id="growl" showDetail="true" sticky="true" />
	<h:head>
		<title>KArtHugo PCs</title>
	</h:head>
	<!-- <marquee scrollamount="40">
		<h1 style="font-size: 100px; color: blue; text-align: left;">Monta
			ai comedia</h1>
		<h:graphicImage url="/images/pc.jpg" />
	</marquee> -->
	<h:body>
		<p:panel header="Cadastro">
			<p:panelGrid columns="2">
				<p:outputLabel value="Nome completo: " for="nome" />
				<p:inputText value="#{usuarioBean.entidade.nome}" required="true"
					id="nome" />
					
				<p:outputLabel value="Username: " for="nick" />
				<p:inputText value="#{usuarioBean.entidade.username}" required="true"
					id="nick" />

				<p:outputLabel value="Email: " for="email" />
				<p:inputText value="#{usuarioBean.entidade.email}" id="email"
					required="true" />

				<p:outputLabel value="Senha: " for="password" />
				<p:password required="true" feedback="true"
					value="#{usuarioBean.entidade.password}" id="password" />

				<p:outputLabel value="Confirme a senha: " for="autPass" />
				<p:password required="true" id="autPass"
					value="#{usuarioBean.autPassword}" />

				<p:column>
					<p:commandButton value="Salvar cadastro" id="btnCadastro"
						action="#{usuarioBean.cadastro()}" update="growl" />
				</p:column>
			</p:panelGrid>
		</p:panel>
	</h:body>
</h:form>
</html>

User class:

package beans;

@Applicationscoped @Named public class Usuariobean Serializable Mplements {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Inject
private UserService service;

private String autPassword;

private Usuario entidade;

protected Collection<Usuario> entidades;

public UsuarioBean() {
}

@PostConstruct
public void init() {
    limpar();
}

public void cadastro() {
    entidades = service.getAll();
    boolean pass = false;
    if (!entidade.getPassword().equals(autPassword)) {
        FacesContext.getCurrentInstance().addMessage("ERROR", new FacesMessage("Confira sua senha!"));
    } else {
        pass = true;
    }
    boolean unique = false;
    for (Usuario u : entidades) {
        if (entidade.getEmail().equals(u.getEmail())) {
            FacesContext.getCurrentInstance().addMessage("ERROR", new FacesMessage("Email já cadastrado"));
        } else {
            unique = true;
        }
    }
    if (pass && unique) {
        entidade.setPassword(service.hash(entidade.getPassword()));
        entidade.setGrupo("USER");
        service.save(entidade);
        limpar();
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
            FacesContext.getCurrentInstance().addMessage("Sucesso!",
                    new FacesMessage("Usuario " + entidade.getNome() + " cadastrado!"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

public String getUserLogin() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    Principal userPrincipal = externalContext.getUserPrincipal();
    if (userPrincipal == null) {
        return "";
    }
    return userPrincipal.getName();
}

public boolean userLogged() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    Principal principal = externalContext.getUserPrincipal();
    if (principal == null) {
        return false;
    } else {
        return true;
    }
}

public void efetuarLogout() throws IOException, ServletException {
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext ec = fc.getExternalContext();
    HttpSession session = (HttpSession) ec.getSession(false);
    session.invalidate();
    HttpServletRequest request = (HttpServletRequest) ec.getRequest();
    request.logout();
    ec.redirect(ec.getApplicationContextPath());
}

public boolean isUserInRole(String role) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    return externalContext.isUserInRole(role);
}

public void remove(Usuario entidade) {
    getService().remove(entidade);
    limpar();
}

public Usuario getEntidade() {
    return entidade;
}

public void setEntidade(Usuario entidade) {
    this.entidade = entidade;
}

public Collection<Usuario> getEntidades() {
    return entidades;
}

public void setEntidades(Collection<Usuario> entidades) {
    this.entidades = entidades;
}

public void save() {
    getService().save(entidade);
    limpar();
}


public void limpar() {
    entidades = getService().getAll();
    entidade = new Usuario();
}


public UserService getService() {
    return service;
}

public String getAutPassword() {
    return autPassword;
}

public void setAutPassword(String autPassword) {
    this.autPassword = autPassword;
}

}

  • Whenever you ask for help solving errors of this type, add the error stack trace to make it easier for you to get help analyzing your problem.

No answers

Browser other questions tagged

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