有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

异步JAXRS实现中的java NullPointerException

我正在尝试实现异步JAX-RS。下面是我的代码:

    @GET
    @Path("/isAlive")
    @Produces(MediaType.TEXT_HTML)
    public void isAlive(@Suspended AsyncResponse ar) {

        executor.execute(() -> {
            System.out.println("Inside");
            Connection conn = ConnectionUtil.getDBConnection();
            PreparedStatement stmt;
            int i = 0;
            try {
                stmt = conn.prepareStatement("insert into TEST_TABLE values(?)");
                stmt.setString(1, "TEST INSERT");
                i = stmt.executeUpdate();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            ar.resume(i);
       });

    }

在执行上述代码时,我在ar.resume(i)处得到一个Nulpointerexception

13:31:35,286 ERROR [stderr] (pool-5-thread-1) Exception in thread "pool-5-thread-1" java.lang.NullPointerException

13:31:35,286 ERROR [stderr] (pool-5-thread-1)   at com.test.PublishWSImpl.lambda$0(PublishWSImpl.java:51)

13:31:35,286 ERROR [stderr] (pool-5-thread-1)   at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

13:31:35,287 ERROR [stderr] (pool-5-thread-1)   at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

我怎么会在这里?如果我删除ar.resume(i)部分,它工作正常。那么我为什么和什么时候应该使用resume()方法呢


共 (0) 个答案