Returning null when trying to display an image in p:graphicImage of Primefaces

Asked

Viewed 815 times

0

I have an error trying to show a photo in the component graphicImage of the Primefaces. The photo can be in Base64 or Bytes. I’m using Primefaces 5.0 and JSF 2.2.

.xhtml

<p:dataTable id="tableFotos" var="foto" value="#{meuBean.listarFotos()}" emptyMessage="meu objeto não possui foto" >
   <p:column headerText="Lista de fotos">
       <p:graphicImage id="photo" value="#{meuBean.getImage(foto.descricao)}" cache="FALSE"/>
       <h:outputText value="#{foto.descricao}"/>
   </p:column>
</p:dataTable>

.java

public StreamedContent getImage(String descricao) {
    for(Foto f : fotos){
        if(f.getDescricao()!=null && f.getDescricao().equals(descricao)){
            byte[] bytes;
            if(f.getFoto()!=null)
                bytes = f.getFoto();
            else
                bytes = Base64.decode(f.getFotoBase64());
            return new DefaultStreamedContent(new ByteArrayInputStream(bytes));//o erro acontece aqui
        }
    }

    return new DefaultStreamedContent();//o erro acontece aqui
}

As I showed even though I have no photo when returning only new DefaultStreamedContent(), the error also happens.

Error message:

java.io.IOException: java.lang.NullPointerException
    org.primefaces.component.graphicimage.GraphicImageRenderer.encodeEnd(GraphicImageRenderer.java:42)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    org.primefaces.component.datatable.DataTableRenderer.encodeCell(DataTableRenderer.java:963)
    org.primefaces.component.datatable.DataTableRenderer.encodeRow(DataTableRenderer.java:923)
    org.primefaces.component.datatable.DataTableRenderer.encodeRows(DataTableRenderer.java:834)
    org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:781)
    org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:744)
    org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:258)
    org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:220)
    org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:84)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:85)
    org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:68)
    org.primefaces.component.panel.PanelRenderer.encodeContent(PanelRenderer.java:204)
    org.primefaces.component.panel.PanelRenderer.encodeMarkup(PanelRenderer.java:121)
    org.primefaces.component.panel.PanelRenderer.encodeEnd(PanelRenderer.java:58)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
    com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
    com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    org.primefaces.component.tabview.TabViewRenderer.encodeTabContent(TabViewRenderer.java:307)
    org.primefaces.component.tabview.TabViewRenderer.encodeContents(TabViewRenderer.java:265)
    org.primefaces.component.tabview.TabViewRenderer.encodeMarkup(TabViewRenderer.java:131)
    org.primefaces.component.tabview.TabViewRenderer.encodeEnd(TabViewRenderer.java:71)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:85)
    org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:68)
    org.primefaces.component.layout.LayoutUnitRenderer.encodeEnd(LayoutUnitRenderer.java:49)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456)
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    com.controllers.LoginFilter.doFilter(LoginFilter.java:41)

root cause

java.lang.NullPointerException
    org.primefaces.util.DynamicResourceBuilder.build(DynamicResourceBuilder.java:50)
    org.primefaces.component.graphicimage.GraphicImageRenderer.getImageSrc(GraphicImageRenderer.java:74)
    org.primefaces.component.graphicimage.GraphicImageRenderer.encodeEnd(GraphicImageRenderer.java:40)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    org.primefaces.component.datatable.DataTableRenderer.encodeCell(DataTableRenderer.java:963)
    org.primefaces.component.datatable.DataTableRenderer.encodeRow(DataTableRenderer.java:923)
    org.primefaces.component.datatable.DataTableRenderer.encodeRows(DataTableRenderer.java:834)
    org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:781)
    org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:744)
    org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:258)
    org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:220)
    org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:84)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:85)
    org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:68)
    org.primefaces.component.panel.PanelRenderer.encodeContent(PanelRenderer.java:204)
    org.primefaces.component.panel.PanelRenderer.encodeMarkup(PanelRenderer.java:121)
    org.primefaces.component.panel.PanelRenderer.encodeEnd(PanelRenderer.java:58)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
    com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
    com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    org.primefaces.component.tabview.TabViewRenderer.encodeTabContent(TabViewRenderer.java:307)
    org.primefaces.component.tabview.TabViewRenderer.encodeContents(TabViewRenderer.java:265)
    org.primefaces.component.tabview.TabViewRenderer.encodeMarkup(TabViewRenderer.java:131)
    org.primefaces.component.tabview.TabViewRenderer.encodeEnd(TabViewRenderer.java:71)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:85)
    org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:68)
    org.primefaces.component.layout.LayoutUnitRenderer.encodeEnd(LayoutUnitRenderer.java:49)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456)
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    com.controllers.LoginFilter.doFilter(LoginFilter.java:41)
  • Primefaces 5.0 and JSF 2.2

1 answer

1

Try to get using 2 Returns, one with a stub of StreamedContent and another with the true image.

public StreamedContent getImage(String descricao) throws IOException {
    FacesContext context = FacesContext.getCurrentInstance();

    for (Foto f : fotos) {
        if ( f.getDescricao() != null && f.getDescricao().equals(descricao) ) {
            if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
                // Aqui renderizamos o HTML. Retornamos um stub do StreamedContent para que ele crie uma URL correta
                return new DefaultStreamedContent();
            } else {
                // Então aqui, o navegador solicita a imagem, retornamos um StreamedContent com os bytes da imagem verdadeira
                byte[] bytes;

                if (f.getFoto() != null) {
                    bytes = f.getFoto();
                } else {
                    bytes = Base64.decode( f.getFotoBase64() );
                }

                return new DefaultStreamedContent(new ByteArrayInputStream(bytes));
            }
        }
    }
}

Additionally, you can try returning the image with a mimetype:

return new DefaultStreamedContent(new ByteArrayInputStream(bytes), "image/jpg");

I found these solutions in this question here.

Browser other questions tagged

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