Search code examples
jsfmanaged-bean

externalContext.getSession(false) returns null


My current platform is NB 7 rc 1 and I have a JSF 2 app with just one "managed bean". In time, I'm using Tomcat 7.0.34.

Here is the code where the error occurs:

 @ManagedBean
    @SessionScoped
    public class CopyController implements Serializable {
      private static final long serialVersionUID = 1L;
      private String pathBancoSentencas;
      private List<Arquivo> arquivosUpload;
      private HttpSession session;
      private List<String> listaPdfs;

      public List<Arquivo> getArquivosUpload() {
        return arquivosUpload;
      }
      public void setArquivosUpload(List<Arquivo> arquivosUpload) {
        this.arquivosUpload = arquivosUpload;
      }

      public CopyController() {
        arquivosUpload = new ArrayList<Arquivo>();
      }

      @PostConstruct
      public void doInit() {
          session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);  
    pathBancoSentencas = (String)session.getAttribute("DIRETORIO_TRABALHO");
  }  

And after processing the request, the routine calls a view like this:

            <p:dataTable value="#{copyController.arquivosUpload}" var="arquivo" paginator="true" paginatorPosition="bottom" 
                         paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}">
              <f:facet name="header">
                Item processado
              </f:facet>
              <h:column>
                <h:outputText value="#{arquivo.nome}" />
              </h:column>
            </p:dataTable>

However, the view is not displayed and this error occurs:

Caused by: java.lang.NullPointerException
    at br.jus.tjmg.dspace.copy.CopyController.doInit(CopyController.java:54)
    ... 70 more

Can someone help me? Thanks!


Solution

  • From your doInit() method,

    getExternalContext().getSession(false);
    

    This returns null if the session is currently not been created yet. But yet you're explicitly expecting a non-null session in the next line.

    You need to pass true to trigger autocreate.

    getExternalContext().getSession(true);
    

    See also the javadoc (emphasis mine):

    If the create parameter is true, create (if necessary) and return a session instance associated with the current request. If the create parameter is false return any existing session instance associated with the current request, or return null if there is no such session.


    Unrelated to the concrete problem, that whole doInit() method is unnecessary and can be replaced by a @ManagedProperty:

    @ManagedProperty("#{DIRETORIO_TRABALHO}")
    private String pathBancoSentencas;
    

    Or, if you're absolutely positive that you need do to it in the doInit(), a nicer way is to get it from ExternalContext#getSessionMap() instead.

    pathBancoSentencas = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("DIRETORIO_TRABALHO");
    

    You should try to avoid javax.servlet.* imports in your JSF backing bean.