title: Error in streaming dynamic resource. null
body: <p><strong>Important Notice :</strong> This issue has been <a href="https://code.google.com/p/primefaces/issues/detail?id=7010" rel="nofollow noreferrer">fixed</a> as of <a href="http://central.maven.org/maven2/org/primefaces/primefaces/5.2/" rel="nofollow noreferrer">PrimeFaces 5.2 final</a> (Community Release) <a href="http://blog.primefaces.org/?p=3491" rel="nofollow noreferrer">released</a> on April 8, 2015. As such if you happened to use that version or newer, you would not need to fiddle around with a temporary workaround.</p>
  
  <p>The earlier given example can now safely be modified as follows.</p>
  
  <pre><code>public StreamedContent getImage() throws IOException {
      FacesContext context = FacesContext.getCurrentInstance();
  
      if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
          return new DefaultStreamedContent();
      } else {
          String id = context.getExternalContext().getRequestParameterMap().get("id");
          byte[] bytes = Utils.isNumber(id) ? service.findImageById(Long.parseLong(id)) : null;
          return bytes == null ? null : new DefaultStreamedContent(new ByteArrayInputStream(bytes));
      }
  }
  </code></pre>
  
  <hr>
  
  <p>I have moved images to the database (MySQL) in the form of BLOB (<code>LONGBLOB</code>) after I got tired of manipulating/managing images stored in the disk file system.</p>
  
  <p>Accordingly, I'm displaying images in <code>&lt;p:dataTable&gt;</code> as follows (blatantly copied from <a href="https://stackoverflow.com/a/12452144/1391249">here</a> :) ).</p>
  
  <pre><code>&lt;p:column headerText="Header"&gt;
      &lt;p:graphicImage value="#{bannerBean.image}" height="200" width="200"&gt;
          &lt;f:param name="id" value="#{row.bannerId}"/&gt;
      &lt;/p:graphicImage&gt;
  &lt;p:column&gt;
  </code></pre>
  
  <p>The bean that retrieves images is as follows.</p>
  
  <pre><code>@ManagedBean
  @ApplicationScoped
  public final class BannerBean
  {
      @EJB
      private final BannerBeanLocal service=null;
      public BannerBean() {}
  
      public StreamedContent getImage() throws IOException {
          FacesContext context = FacesContext.getCurrentInstance();
  
          if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
              return new DefaultStreamedContent();
          }
          else {
              String id = context.getExternalContext().getRequestParameterMap().get("id");
              byte[] bytes = service.findImageById(Long.parseLong(id));
              return bytes==null? new DefaultStreamedContent():new DefaultStreamedContent(new ByteArrayInputStream(bytes));
          }
      }
  }
  </code></pre>
  
  <p>This works fine as long as there are images in each row of the underlying database table. </p>
  
  <p>The BLOB type column in the database is however, optional in some cases and hence, it can contain <code>null</code> values as well.</p>
  
  <p>If this column in any row/s in the database is <code>null</code> then, the following exception is thrown.</p>
  
  <pre><code>SEVERE:   Error in streaming dynamic resource. null
  WARNING:   StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
  java.lang.NullPointerException
      at org.primefaces.application.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:127)
      at javax.faces.application.ResourceHandlerWrapper.handleResourceRequest(ResourceHandlerWrapper.java:153)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:643)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
      at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
      at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
      at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
      at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
      at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
      at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
      at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
      at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
      at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
      at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
      at java.lang.Thread.run(Thread.java:722)
  </code></pre>
  
  <p>So how to manage <code>null</code> BLOBs so that this exception disappears?</p>
  
  <p>Returning <code>new DefaultStreamedContent(new ByteArrayInputStream(new byte[0]))</code>, in case, the byte array in the managed bean is null would suppress the exception but this after all, should not be a solution. Is this a desired solution? </p>
  
  <hr>
  
  <p>The EJB method that returns a byte array though completely unnecessary, in this case.</p>
  
  <pre><code>public byte[] findImageById(Long id)
  {
      CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
      CriteriaQuery&lt;byte[]&gt;criteriaQuery=criteriaBuilder.createQuery(byte[].class);
      Root&lt;BannerImages&gt; root = criteriaQuery.from(BannerImages.class);
      criteriaQuery.multiselect(root.get(BannerImages_.bannerImage));
      ParameterExpression&lt;Long&gt;parameterExpression=criteriaBuilder.parameter(Long.class);
      criteriaQuery.where(criteriaBuilder.equal(root.get(BannerImages_.bannerId), parameterExpression));
      List&lt;byte[]&gt; list = entityManager.createQuery(criteriaQuery).setParameter(parameterExpression, id).getResultList();
      return list!=null&amp;&amp;!list.isEmpty()?list.get(0):null;
  }
  </code></pre>