java.lang.NullPointerException on chat system

0

I'm doing a HelpDesk chat project with JSF 2.0 and Primefaces, but an error is thrown when I try to connect the Attendant or user in the chat.

In atendente.xhtml and index.xhtml has a form that sends the information to AtendenteBean.java and UsuarioBean.java , respectively.

Attendant.jsp:

<p:panel header="Login do Atendente" rendered="#{not atendenteMB.autenticado}">
         <h:outputLabel for="txtNome" value="Login:"/>
         <br/>

         <h:inputText id="txtNome" value="#{atendenteMB.atendente.login}"/>
         <br/>

         <h:outputLabel for="txtSenha" value="Senha:"/>
         <br/>

         <h:inputSecret id="txtSenha" value="#{atendenteMB.atendente.senha}"/>
         <br/>
         <h:commandButton action="#{atendenteMB.signIn}" value="Entrar"/>
         <br/>

       </p:panel>

AttendantBean.java:

@ManagedBean(name="atendenteMB")

@SessionScoped
public class AtendenteBean implements Serializable{

public AtendenteBean(){
}

private static final long serialVersionUID = 1L;

@ManagedProperty(value="#{localizadorMB}")
private LocalizadorBean gerenciarLocalizador;

@ManagedProperty(value="#{chatMB}")
private ChatBean chat;

private Atendente atendente = new Atendente();
private boolean autenticado;

public void signIn(){
    if(atendente.getLogin().equals(atendente.getSenha())){
        gerenciarLocalizador.getGerenciador().addAtendente(atendente);
        setAutenticado(true);
    }
    else{
        FacesMessage msgm = new FacesMessage("Login ou Senha inválidos.");
        FacesContext.getCurrentInstance().addMessage(null, msgm);
    }

}

Error generated when I click the Send button.

HTTP Status 500 - java.lang.NullPointerException

type Exception report

message
---
java.lang.NullPointerException

description
---
The server encountered an internal error that prevented it from fulfilling this request.

exception 
---
javax.servlet.ServletException: java.lang.NullPointerException
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)



root cause 
---
javax.faces.el.EvaluationException: java.lang.NullPointerException
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpress    ionAdapter.java:101)
        com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251)
        com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)



root cause
---

java.lang.NullPointerException
    managedbean.AtendenteBean.signIn(AtendenteBean.java:34)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.apache.el.parser.AstValue.invoke(AstValue.java:278)
    org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
    com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
        javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpress    ionAdapter.java:87)
        com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251)
        com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)



note The full stack trace of the root cause is available in the Apache Tomcat/7.0.47 logs.

If you need more details, just comment.

    
asked by anonymous 08.02.2014 / 16:59

1 answer

1

Debug for what I want ..

But looking over, there are 3 lines, within the signIn () method that may be referencing null references:

1 - Attendant.getLogin (). equals (...)
2 - manageLocalizador.getGerenciador (). AddAtendente (attendant)
3 - FacesContext.getCurrentInstance (). AddMessage (null, msgm)

If getLogin (), getGerender () or getCurrentInstance () return a null reference, it is not possible to access methods or properties of the object, since it is not where it should be

You can solve this in two ways: by running a debug and checking the return of each of these 3 methods above, or what is more indicated; allocate local variables within the scope of the signIn function and check for nullity of the object before accessing its members in a nested way like you did.

Login l = atendente.getLogin();
if(l == null) {
 ... // imprime no console.. fonte do bug
  return; // retorna abruptamente
}

// ok, temos certeza que "f" existe
if (l.equals(...)) {...}

Gerenciador g = gerenciarLocalizador.getGerenciador();
if(g == null) {
 // imprime no console.. fonte do bug
 return; // retorna abruptamente
}

// ok, agora temos certeza que "g" existe
g.addAtendente(atendente);

FacesContext context = FacesContext.getCurrentInstance()

if(context != null) { ... }

If you want after detecting which of these null backs and correcting the error (they have been created somewhere else), you can return your code as it was, in a nested way if you wish, but as long as the objects are always guaranteed have been instantiated before (although not a good practice)

    
09.02.2014 / 19:48