Primefaces, p:fileDownload ajax="false" nullPointerException

Asked

Viewed 162 times

2

Hello, I’m new here, I’ve searched for Google of life and here in Stack something similar but I can’t find. I’m not as experienced as DEV so who can help...

The problem is the p:DownloadFile. At least I believe it is there. It gives a 500 error of nullpointerException and the log apparently says nothing. I use the primefaces framework. In xhtml I have the datatable and button column:

    <p:dataTable value="#{lancamentoRCCadastroMB.arquivos}" var="arquivo" styleClass="small"   
                                scrollable="true"  scrollHeight="150" id="gridNotaFiscalDigiRC"
                                emptyMessage="Nenhum registro encontrado."
                                rowSelectMode="click" rowHover="true">

            <p:column headerText="Arquivo" styleClass="align-center" >
                    <p:commandButton id="btnDownload2RC" ajax="false" actionListener="#{lancamentoRCCadastroMB.downloadAnexoLancamento(arquivo.codigo)}"
                                            icon="fa fa-download state-primary" title="Download" value="#{arquivo.nome}"
                                            styleClass="rowClick" immediate="true" process="gridNotaFiscalDigiRC" rendered="#{lancamentoRCCadastroMB.analistaOrcamentario}">
                        <p:fileDownload value="#{lancamentoRCCadastroMB.streamedContentAnexoLancamento}" />                     
                    </p:commandButton>              
            </p:column>
    </p:dataTable>

In the bean:

public void downloadAnexoLancamento(Integer pCodigo) {

    ArquivoLancamentoVO lArquivo = new ArquivoLancamentoVO();
    if(arquivos != null && !arquivos.isEmpty()) {

        for(ArquivoLancamentoVO lArquivoLoop : arquivos) {
            if(pCodigo.equals(lArquivoLoop.getCodigo())) {
                lArquivo = lArquivoLoop;
                break;
            }
        }

        if(lArquivo.getCodigo() != null) {
            ByteArrayInputStream stream = new ByteArrayInputStream(lArquivo.getArquivo());
            streamedContentAnexoLancamento = new DefaultStreamedContent(stream, "application/x-download", lArquivo.getNome());
        }           
    }
}

It turns out that by clicking the button id="btnDownload2RC", should call the download bean method and prepare the file for download. Debugging the method is not invoked when the ajax="false". If I put it to true it calls the method but does not download the file. I’ve heard that these processes and prime updates may be getting in my way, but I’ve checked the code and I can’t find the flaw.

1 answer

0

It may be that Listener is being called after running the getStreamedContentAnexoLancamento(), therefore do not create streamed content. Try to make these modifications:

In XHTML:

<p:commandButton
    id="btnDownload2RC"
    icon="fa fa-download state-primary"
    title="Download"
    value="#{arquivo.nome}"
    ajax="false"
    styleClass="rowClick"
    immediate="true"
    process="gridNotaFiscalDigiRC"
    rendered="#{lancamentoRCCadastroMB.analistaOrcamentario}"
    >
    <p:fileDownload value="#{lancamentoRCCadastroMB.downloadAnexoLancamento(arquivo.codigo)}" />
</p:commandButton>

No Bean:

public StreamedContent downloadAnexoLancamento(Integer pCodigo) {

    ArquivoLancamentoVO lArquivo = new ArquivoLancamentoVO();

    if(arquivos != null && !arquivos.isEmpty()) {

        for (ArquivoLancamentoVO lArquivoLoop : arquivos) {
            if(pCodigo.equals(lArquivoLoop.getCodigo())) {
                lArquivo = lArquivoLoop;
                break;
            }
        }

        if(lArquivo.getCodigo() != null) {
            ByteArrayInputStream stream = new ByteArrayInputStream(lArquivo.getArquivo());
            streamedContentAnexoLancamento = new DefaultStreamedContent(stream, "application/x-download", lArquivo.getNome());
        }           
    }

    return streamedContentAnexoLancamento;
}

Browser other questions tagged

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