So, I have this method which marshals an object.
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;
}
I dont know why it is throwing null pointer exception at getJaxb2Marshaller().marshal(messageObject, sw); And Over that, when I tried to sysout the finalstring, it is giving a legimitate output but the method itself is returning Null. I put a debug on code and found, the null pointer is because of StreamResult which is null. I have no idea, why it is coming like that. Please help!