1
 private void respond(String response) {

    httpServletResponse.setContentType("application/json");
    httpServletResponse.setCharacterEncoding("UTF-8");

    try {
        PrintWriter out = httpServletResponse.getWriter();

        out.print(response);
        out.flush();

    } catch (IOException ex) {
        log.error("Error while writing response",ex);
    }

}

This code throws null pointer at out.flush(). httpServletResponse is instance variable. I have verified that response is not null. This function is being called from inside Apache's aync http client lib's CloseableHttpAsyncClient.execute.completed().

LOG:

Caused by: java.lang.NullPointerException: null
    at org.apache.coyote.http11.InternalNioOutputBuffer.flushBuffer(InternalNioOutputBuffer.java:234) ~[tomcat-coyote.jar:8.0.14]
    at org.apache.coyote.http11.InternalNioOutputBuffer.addToBB(InternalNioOutputBuffer.java:189) ~[tomcat-coyote.jar:8.0.14]
    at org.apache.coyote.http11.InternalNioOutputBuffer.commit(InternalNioOutputBuffer.java:177) ~[tomcat-coyote.jar:8.0.14]
    at org.apache.coyote.http11.AbstractHttp11Processor.action(AbstractHttp11Processor.java:739) ~[tomcat-coyote.jar:8.0.14]
    at org.apache.coyote.Response.action(Response.java:179) ~[tomcat-coyote.jar:8.0.14]
    at org.apache.coyote.Response.sendHeaders(Response.java:341) ~[tomcat-coyote.jar:8.0.14]
    at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:331) ~[catalina.jar:8.0.14]
    at org.apache.catalina.connector.OutputBuffer.flush(OutputBuffer.java:313) ~[catalina.jar:8.0.14]
    at org.apache.catalina.connector.CoyoteWriter.flush(CoyoteWriter.java:98) ~[catalina.jar:8.0.14]
    at com.thruport.aje.rotator.reward.async.RewardRequest.respond(RewardRequest.java:185) ~[output/:na]
    at com.thruport.aje.rotator.reward.async.RewardRequest.lambda$new$2(RewardRequest.java:131) ~[output/:na]
    at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:602) ~[na:1.8.0_121]
    ... 8 common frames omitted
Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
rentedrainbow
  • 184
  • 2
  • 10
  • 2
    "This code throws null pointer at `out.flush()`." => No way! It would throw an NPE at line `out.print(response)` first. – Seelenvirtuose Jun 09 '17 at 17:12
  • 1
    Do you have the Stacktrace? Please, edit your question and insert it. – Danilo Guimaraes Jun 09 '17 at 17:13
  • it is impossible for `.flush()` to throw an NPE if the line right above succeeded. Your step debugger would show this fact as well as what is actually causing the NPE. –  Jun 09 '17 at 17:15
  • 1
    [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Jun 09 '17 at 17:15
  • Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. –  Jun 09 '17 at 17:15
  • @DaniloGuimaraes added logs – rentedrainbow Jun 09 '17 at 17:35
  • @JarrodRoberson why do you think I wouldn't have done those obvious verifications before asking here? – rentedrainbow Jun 09 '17 at 17:37
  • 2
    Guys, stop saying _"it's impossible to throw an NPE here and there"_. The `PrintWriter` instance cames from `httpServletResponse.getWriter();`. So, it's not necessary the default implementation of `flush` of `PrintWriter`. [Reading the Coyote code here](http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tomcat.embed/tomcat-embed-core/8.0.24/org/apache/coyote/http11/InternalNioOutputBuffer.java#InternalNioOutputBuffer.flushBuffer%28boolean%29), it's probably a bug on Apache Coyote (I don't know). – Danilo Guimaraes Jun 09 '17 at 17:48
  • In order to answer the question, we need more details. Include a description of the class of which the `respond` method is part and how the global variable `httpServletResponse` is initialized. From the few details that you have included, it appears that the httpServletResponse variable is not null, but has not been correctly initialized. – DwB Jun 09 '17 at 18:09
  • Thank you everyone for help. It got resolved. – rentedrainbow Jun 10 '17 at 07:06

1 Answers1

0

It turns out I shouldn't be flushing here as Servlet Container will take care of it at end of Servlet lifecycle and that Buffer wont be ready for flush at this stage. this Nullpointer exception while flushing ServletOutputStream and this Should one call .close() on HttpServletResponse.getOutputStream()/.getWriter()? answers helped.

rentedrainbow
  • 184
  • 2
  • 10
  • Hi. Nice it woks for you. But I must say that this is not a resolution. we should be able to call flush(). – mba Jan 21 '19 at 10:09