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)