JaxB2Marshaller抛出Null指针异常

时间:2016-11-22 12:21:01

标签: java jaxb stringwriter

所以,我有这个方法来整理一个对象。

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 finalstring时,它给出了一个legimitate输出,但方法本身返回Null。 我对代码进行了调试并发现,空指针是因为StreamResult是null。我不知道,为什么会这样。请帮忙!

0 个答案:

没有答案