0

所以,我有这个方法来编组一个对象。

public String marshalResponse(Object messageObject) {
    if (messageObject == null) {
        return null;
    }
    String finalstring ="No results found!";
    try {

        StringWriter sw = new StringWriter();
        StreamResult streamResult = new StreamResult(sw);
        getJaxb2Marshaller().marshal(messageObject, streamResult);
        StringWriter sw2 = (StringWriter) streamResult.getWriter();
        StringBuffer sb = sw2.getBuffer();
        finalstring = sb.toString();
        System.out.println("Response:"+finalstring);
        return finalstring;
    } catch (Exception ex) {
        log.warn("marshalObject(): Exception caught while trying to marshal an object (object.class.name="
                + messageObject.getClass().getName() + ", ex=" + ex + ")");

    }
    return null;
}

getJaxb2Marshaller()

    public Jaxb2Marshaller getJaxb2Marshaller() {
    return jaxb2Marshaller;
}

我不知道为什么它在getJaxb2Marshaller().marshal(messageObject, sw); 抛出空指针异常; 除此之外,当我尝试 sysout 最终字符串时,它给出了合法的输出,但方法本身返回 Null。我对代码进行了调试,发现空指针是因为 StreamResult 为空。我不知道,为什么会这样。请帮忙!

4

0 回答 0