0

I'm using the same code to show an image using p:graphicImage, but I can't find why it's not working.

I was using this code below because in my application it's not mandatory to have an image in every db entry, so i writed it this way to avoid null blobs.

Now i rewrited it to be exactly as answered in Display dynamic image from database with p:graphicImage and StreamedContent but it still doesn't work :(

I'm using the exact same code for showing images in an exact same p:repeat in another .xhtml and it's working perfectly.

During debugging sessions i see that it calls one other ManagedBean that has not any reference in the .xhtml i'm working on.



    @ManagedBean
    @ViewScoped
    public class PatenteService implements Serializable {

        private StreamedContent fotoPatente;

        public StreamedContent getFotoPatente() throws IOException {
            String inputId = "";
            Patente p;
            byte[] bytes;
            FacesContext context = FacesContext.getCurrentInstance();

            byte[] defaultBytes; // ... Retrieve deafult image to show if db resgistry has it not

            if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
                return new DefaultStreamedContent(new ByteArrayInputStream(defaultBytes));
            } else {
                inputId = context.getExternalContext().getRequestParameterMap().get("patenteId");

                if (!inputId.equals("")) {
                    p = (Patente) EManager.getInstance().createNamedQuery("Patente.findById").setParameter("id", Integer.parseInt(inputId)).getSingleResult();
                    bytes = p.getFoto();
                } else {
                    bytes = null;
                }

                if (bytes != null) {
                    return new DefaultStreamedContent(new ByteArrayInputStream(bytes));
                } else {
                    return new DefaultStreamedContent(new ByteArrayInputStream(defaultBytes));
                }
            }
        }

        public void setFotoPatente(StreamedContent fotoPatente) {
            this.fotoPatente = fotoPatente;
        }
    }

Errors:

01-Sep-2017 18:13:19.716 GRAVE [http-nio-8080-exec-7] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [Faces Servlet] in context with path [/prorec] threw exception
 java.io.IOException: java.lang.NullPointerException
    at org.primefaces.application.resource.StreamedContentHandler.handle(StreamedContentHandler.java:103)
    at org.primefaces.application.resource.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:95)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:643)
...
Caused by: java.lang.NullPointerException
    at com.sun.faces.mgbean.BeanManager$ScopeManager$ViewScopeHandler.getFromScope(BeanManager.java:566)
    at com.sun.faces.mgbean.BeanManager$ScopeManager.getFromScope(BeanManager.java:477)
01-Sep-2017 18:13:19.868 GRAVE [http-nio-8080-exec-4] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [Faces Servlet] in context with path [/prorec] threw exception
 java.io.IOException: java.lang.NullPointerException
    at org.primefaces.application.resource.StreamedContentHandler.handle(StreamedContentHandler.java:103)
    at org.primefaces.application.resource.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:95)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:643)
...
Caused by: java.lang.NullPointerException
    at com.sun.faces.mgbean.BeanManager$ScopeManager$ViewScopeHandler.getFromScope(BeanManager.java:566)
    at com.sun.faces.mgbean.BeanManager$ScopeManager.getFromScope(BeanManager.java:477)
    at com.sun.faces.mgbean.BeanManager.getBeanFromScope(BeanManager.java:240)

01-Sep-2017 18:13:19.715 GRAVE [http-nio-8080-exec-7] org.primefaces.application.resource.StreamedContentHandler.handle Error in streaming dynamic resource.
 java.lang.NullPointerException
    at com.sun.faces.mgbean.BeanManager$ScopeManager$ViewScopeHandler.getFromScope(BeanManager.java:566)
    at com.sun.faces.mgbean.BeanManager$ScopeManager.getFromScope(BeanManager.java:477)
    at com.sun.faces.mgbean.BeanManager.getBeanFromScope(BeanManager.java:240)
...
01-Sep-2017 18:13:19.867 GRAVE [http-nio-8080-exec-4] org.primefaces.application.resource.StreamedContentHandler.handle Error in streaming dynamic resource.
 java.lang.NullPointerException
    at com.sun.faces.mgbean.BeanManager$ScopeManager$ViewScopeHandler.getFromScope(BeanManager.java:566)
    at com.sun.faces.mgbean.BeanManager$ScopeManager.getFromScope(BeanManager.java:477)
    at com.sun.faces.mgbean.BeanManager.getBeanFromScope(BeanManager.java:240)
  • I've already rewrited it exactly as you say but it still only works for one of my .xhtml, for the other one it doesn't :( – Douglas Soares Sep 01 '17 at 21:37
  • 1
    **Read** the answer instead of only copypasting code snippets. Your bean is still @ViewScoped and you're still getting hold of StreamedContent as instance variable. The answer clearly explains why that does not work at all. – BalusC Sep 01 '17 at 22:22
  • @BalusC I did read and i didn't copypasted it. But in a debugging session i saw that even changing the value to (being imageBean a new ApplicationScoped class i created), the primefaces continues to call the old method of the ViewScoped bean. i've "cleaned and dist" again but it doesn't call the new method. Why does it happens and how do i fix it? (i can't update the code and errors right now because i got some affairs. but if needed i'll do it monday) – Douglas Soares Sep 03 '17 at 01:05

0 Answers0