与HttpServletResponse相关的空指针异常

时间:2013-05-25 19:29:57

标签: jsp nullpointerexception liferay portlet

我想从我的网络应用程序中打开一个文件 我有一个jsp,在这个代码中,当我点击按钮时它应该打开一个文件, 我的代码jsp(特别是javascript代码)调用java代码,但我有一个错误:Null-pointer Exception

我认为此错误通常是因为我将response变为null, 但我不知道何时填写responseHttpServletResponse response

我的JSP代码如下:

<div id="pieces" class="baliseDiv"> 
    <fieldset style="padding: 5px 10px 5px 10px;">
        <legend align="top">
            <util:message key="operateur.section.pieces.title" />
        </legend>

    <grid:view name="PieceRattacheList_view">
        <grid:search position="top" align="right" width="200"
                iconClass="icon-zoom" />

        <grid:actionsbar>
            <grid:action id="pieceRattacheList_add" type="button"
                    textkey="operateur.list.pieceRattacheList.action.pieceRattacheList_add.title"
                    tooltipkey="operateur.list.pieceRattacheList.action.pieceRattacheList_add.description"
                    iconClass="icon-form-add" onclick="action_pieceRattacheList_add" />

            <grid:action id="pieceRattacheList_add_Split" type="splitbar" />
        </grid:actionsbar>

        <grid:pagingbar pageSize="10" displayInfo="true">
        </grid:pagingbar>

        <grid:table name="PieceRattacheList_Tab" list="pieceRattacheList"
                titlekey="operateur.list.pieceRattacheList.title"
                formbean="DossierJuridiquePortletBean" recordForm="false"
                clicksToEdit="1" collapsible="true" closable="true" render="true">

            <grid:repository id="pieceRattacheList.pieceRattacheList"
                    classpath="com.dq.foprodex.web.repository.DossierJuridiqueRepository" />

            <grid:record id="attachementEntity" idProperty="id"
                    defaultSortColumn="nomfrPiece" defaultSortDir="ASC"
                    title="Enregistrement" columnCount="2"
                    iconClass="icon-edit-record"
                    recordClass="com.dq.foprodex.web.entity.AttachementEntity"
                    windowmodal="false">
                <grid:property name="id" type="String" />
                <grid:property name="idElement" type="String" />
                <grid:property name="idTable" type="String" />
                <grid:property name="idGuid" type="String" />
                <grid:property name="nomfrPiece" type="String" />
                <grid:property name="nomarPiece" type="String" />
                <grid:property name="etat" type="String" />
                <grid:property name="fo" type="String" />
                <grid:property name="df" type="String" />
                <grid:property name="agBack" type="String" />
                <grid:property name="respBack" type="String" />
                <grid:property name="pdg" type="String" />
                <grid:property name="idType" type="String" />
                <grid:property name="libType" type="String" />
                <grid:property name="respRattach" type="String" />
                <grid:property name="dateRattach" type="String" />
                <grid:property name="nbreRappel" type="Integer" />
                <grid:property name="dateDernRappel" type="String" />
                <grid:property name="dateDemComp" type="String" />
                <grid:property name="dateDepotComp" type="String" />
                <grid:property name="idAvis" type="String" />
                <grid:property name="libAvis" type="String" />
                <grid:property name="comm" type="String" />
                <grid:property name="app" type="String" />
                <grid:property name="dateApp" type="String" />
                <grid:property name="avisColumn" type="String" />
                <grid:property name="motif" type="String" />
                <grid:property name="rattachement" type="String" />
                <grid:property name="actiondocumentdiffuser" type="String" />
                <grid:property name="form" />
            </grid:record>

            <grid:column property="nomfrPiece"
                    titlekey="operateur.list.pieceRattacheList.column.nomfrPiece.title"
                    sortable="true" resizable="true" menuDisabled="false" width="20">
            </grid:column>
            <grid:column property="rattachement"
                    titlekey="operateur.list.pieceRattacheList.column.rattachement.title"
                    sortable="true" resizable="true" menuDisabled="false" width="40">
            </grid:column>
            <grid:column property="respRattach"
                    titlekey="operateur.list.pieceRattacheList.column.respRattach.title"
                    sortable="true" resizable="true" menuDisabled="false" width="40">
            </grid:column>
            <grid:column property="dateRattach"
                    titlekey="operateur.list.pieceRattacheList.column.dateRattach.title"
                    sortable="true" resizable="true" menuDisabled="false" width="40">
            </grid:column>
            <grid:column property="nbreRappel"
                    titlekey="operateur.list.pieceRattacheList.column.nbreRappel.title"
                    sortable="true" resizable="true" menuDisabled="false" width="40">
            </grid:column>
            <grid:column property="dateDernRappel"
                    titlekey="operateur.list.pieceRattacheList.column.dateDernRappel.title"
                    sortable="true" resizable="true" menuDisabled="false" width="40">
            </grid:column>
            <grid:column property="etat"
                    titlekey="operateur.list.pieceRattacheList.column.etat.title"
                    sortable="true" resizable="true" menuDisabled="false" width="40">
            </grid:column>
            <grid:column property="comm"
                    titlekey="operateur.list.pieceRattacheList.column.comm.title"
                    sortable="true" resizable="true" menuDisabled="false" width="40">
            </grid:column>

            <grid:column property="rowaction2" width="5">
                <grid:rowaction id="piece"
                        tooltipkey="operateur.list.pieceRattacheList.action.pieceRattacheList_update1.title"
                        iconClass="icon-form-key1"
                        onclick="action_consultDoc"/>
            </grid:column>
        </grid:table>
    </grid:view>

    <br>

    </fieldset>
</div>

<script type="text/javascript">

function action_consultDoc(grid, record, row, col) {
    operateurService.consultDocService(Ext.getCmp('userId').getValue(),record.get('id'), {
        callback : function(responseEntity) {

        }
    });
}
</script>

班级consultDocService的方法operateurService是:

public void consultDocService(String userid, String id) {

    DossierJuridiqueManager dossierJuridiqueManager =
            (DossierJuridiqueManager) PortalUtil.getBean(DossierJuridiqueManager.managerid);

    try {
        dossierJuridiqueManager.consultDoc(null, null, userid, id);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我班consultDoc的代码DossierJuridiqueManager

public ResponseEntity consultDoc(HttpServletRequest request, HttpServletResponse response, String userid, String id)
        throws Exception {
    System.out.println("the ids"+id);
    List<AttachementEntity> attachements = attachementDAO.getAttachementList(id);
    AttachementEntity attachementEntity=new AttachementEntity();
    attachementEntity=attachements.get(0);
    String guid=attachementEntity.getIdGuid();

    P8Connector connector = new P8Connector(TypeEnum.CE.value(), "gcd admin","C:/properties/");
    System.out.println("verif " + connector.getConnection());

    GetDocumentsListInFolder getDocumentsListInFolder = new GetDocumentsListInFolder();
    List<Document> list = new ArrayList<Document>();

    list = getDocumentsListInFolder.execute(connector, "/chemin", "1");

    int length = 0;
    ServletOutputStream op = response.getOutputStream();

    for (int i = 0; i < list.size(); i++) {
        Document doc = (Document) list.get(i);

        String h = doc.get_Id().toString();
        if(h.equals(guid))
        {
            GetDocumentContent c = new GetDocumentContent();

            byte[] is1 = c.execute(connector, doc);

            byte[] bbuf = new byte[1024];
            InputStream is3 = new ByteArrayInputStream(is1);
            DataInputStream in = new DataInputStream(is3);

            while ((in != null) && ((length = in.read(bbuf)) != -1)) {
                op.write(bbuf, 0, length);
            }

            in.close();
            op.flush();
            op.close();
        }
    }

    return new ResponseEntity();
}

错误NullPointerException与此行相关:

ServletOutputStream op = response.getOutputStream();

这是因为我通过以下代码将response发送到null

dossierJuridiqueManager.consultDoc(null,null, userid, id);

但我应该何时填写response

1 个答案:

答案 0 :(得分:0)

你必须在哪个地方有一个叫你服务的控制器,对吧?因此,您可以通过服务将Response对象从控制器中的处理程序方法传递给DossierJuridiqueManager。

最好从DossierJuridiqueManager类返回文件内容,然后在Controller中执行I / O工作。这将使控制器中的响应保持在它所属的位置。