1

I'm trying do show a pdf in a JSF page but when the render occurs i have this problem as below. I have already tried a lot of solutions, but with no results. I'm opened to different modes to show the pdf, as the p:media (this one not worked neither).

Trouble:

    GRAVE: Error in streaming dynamic resource.
java.lang.NullPointerException
    at br.com.graflogic.utilities.presentationutil.scope.ViewScope.get(ViewScope.java:19)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:327)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1119)
    at org.springframework.beans.factory.access.el.SpringBeanELResolver.getValue(SpringBeanELResolver.java:55)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
    at org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:179)

ViewScope:

@Override
public Object get(String name, @SuppressWarnings("rawtypes") ObjectFactory objectFactory) {
    Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();

    if (viewMap.containsKey(name)) {
        return viewMap.get(name);
    } else {
        Object object = objectFactory.getObject();
        viewMap.put(name, object);

        return object;
    }
}

Controller:

try {

            final ByteArrayOutputStream out = new ByteArrayOutputStream();

            final Document document = new Document();
            PdfWriter.getInstance(document, out);
            document.open();

            for (int i = 0; i < 50; i++) {
                document.add(new Paragraph("All work and no play makes Jack a dull boy"));
            }

            document.close();
            content = new DefaultStreamedContent(new ByteArrayInputStream(out.toByteArray()), "application/pdf");

        } catch (final Exception e) {
            e.printStackTrace();
        }

Page:

            <pe:documentViewer height="500" value="#{relatorioController.content}" download="#{relatorioController.downloadFileName}"/>  

Application:

PDF.js v1.5.188 (build: 0e2d50f) Mensagem: Unexpected server response (500) while retrieving PDF

Kukeltje
  • 11,924
  • 4
  • 19
  • 44
vmar
  • 29
  • 3
  • This has no relation to Spring, just JSF and things not being serializable. – M. Deinum Apr 15 '20 at 13:21
  • Still nothing spring related, still JSF and something that isn't serializable. You are using view-scoped stuff, which gets put in the http-session and for that to work things need to be serializable. The error indicates things aren't hence the error. Not Spring related, not document viewer related, just basic view scope attribute not being serializable – M. Deinum Apr 15 '20 at 13:26
  • not related to JSF, PrimeFaces or documentviewer either. Sometjing not being serializable is a custom code thing – Kukeltje Apr 15 '20 at 13:51
  • 1
    Can you post all the view scoped bean code? – WoAiNii Apr 15 '20 at 17:21
  • Sure @WoAiNii https://www.codepile.net/pile/2LzlxYeg – vmar Apr 15 '20 at 17:39
  • 1
    I'm missing something, where do you use that custom scope in this page? (Really a strange name, can lead to many misunderstanding) The error points to Object object = objectFactory.getObject(); row, I never use a custom scope, but in this [guide](https://www.baeldung.com/spring-custom-scope) it says you need to register your scope, do you do that? And as a suggestion, why do you need all these customizations, why don't you just replicate the [showcase](https://www.primefaces.org/showcase/ui/multimedia/media.xhtml) logic? – WoAiNii Apr 15 '20 at 18:19
  • 1
    After the edit it is fully spring related and still not JSF, PrimeFaces or PF extenstions. You use a custom Spring based `@ViewSCoped`... The error is in there. And code should be inline, not in remote repositories... And effectively you chameleonized the question by posting a completey different error/stacktrace in it. Bad practice. Either remove the question and create a new one or answer how you solved the initial problem, otherwise you would not have gotten the new problem. – Kukeltje Apr 16 '20 at 07:37
  • 1
    Effectively your question now is a duplicate of https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Kukeltje Apr 16 '20 at 07:40
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Kukeltje Apr 16 '20 at 07:41

0 Answers0