# Transport client throwing NPE when closing

**URL:** <https://discuss.elastic.co/t/transport-client-throwing-npe-when-closing/176648>\
**Category:** Elasticsearch\
**Created:** [April 12, 2019, 2:33pm UTC](https://discuss.elastic.co/t/transport-client-throwing-npe-when-closing/176648 "2019-04-12T14:33:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Gatihi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_gatihi/32/25820_2.png) [@Chris\_Gatihi](https://discuss.elastic.co/u/Chris_Gatihi)\
**Post date:** [April 12, 2019, 2:33pm UTC](https://discuss.elastic.co/t/transport-client-throwing-npe-when-closing/176648/1 "2019-04-12T14:33:40Z")

</div>

Hi,

I'm using ES and Transport 5.5.2 and getting an NPE thrown when closing the transport client:

```
// Shut down existing client if open
    if (client != null) {
      logger.info("Detected open Transport client - closing it");
      try {
        client.close();
        if (client.threadPool() != null)
          client.threadPool().shutdown();
      } catch (Exception e) {
        String message = "An exception was thrown when attempting to close the Transport client: ";
        logger.error(message, e);

```

Here's what that logger entry in the last line produces:

`message: An exception was thrown when attempting to close the Transport client: stack_trace: java.lang.NullPointerException: null`

Note that the client and threadpool objects themselves aren't null since I'm checking for null so it seems to be something internal to either `TransportClient.close()` or `ThreadPool.shutdown()` that's causing this.

Any ideas?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 12, 2019, 2:47pm UTC](https://discuss.elastic.co/t/transport-client-throwing-npe-when-closing/176648/2 "2019-04-12T14:47:27Z")

</div>

Why are you manually closing the Threadpool. It's already done by the close method AFAIK:

> <https://github.com/elastic/elasticsearch/blob/5.6/core/src/main/java/org/elasticsearch/client/transport/TransportClient.java#L350-L362>

---

<div class="post-metadata">

**Author:** ![Chris\_Gatihi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_gatihi/32/25820_2.png) [@Chris\_Gatihi](https://discuss.elastic.co/u/Chris_Gatihi)\
**Post date:** [April 12, 2019, 3:10pm UTC](https://discuss.elastic.co/t/transport-client-throwing-npe-when-closing/176648/3 "2019-04-12T15:10:15Z")

</div>

Thanks @dadoonet. I think it was in documentation somewhere. I can remove that. Do you think that could be causing the problem? i.e. we are trying to close the thread pool after it has already been closed (and nullified?) by the call to `TransportClient.close()`?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 12, 2019, 3:11pm UTC](https://discuss.elastic.co/t/transport-client-throwing-npe-when-closing/176648/4 "2019-04-12T15:11:34Z")

</div>

Yeah may be.

---

<div class="post-metadata">

**Author:** ![Chris\_Gatihi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_gatihi/32/25820_2.png) [@Chris\_Gatihi](https://discuss.elastic.co/u/Chris_Gatihi)\
**Post date:** [April 15, 2019, 6:51am UTC](https://discuss.elastic.co/t/transport-client-throwing-npe-when-closing/176648/5 "2019-04-15T06:51:10Z")

</div>

Thanks for the pointer @dadoonet. Removing the code that was closing the threadpools manually got rid of the NPE. But now I'm seeing an `IllegalStateException` when I call `TransportClient.close()`:

```
java.lang.IllegalStateException: thread was not started
 	at io.netty.util.concurrent.GlobalEventExecutor.awaitInactivity(GlobalEventExecutor.java:197)
 	at org.elasticsearch.xpack.client.PreBuiltXPackTransportClient.close(PreBuiltXPackTransportClient.java:63)

```

Have you seen such before?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 15, 2019, 7:29am UTC](https://discuss.elastic.co/t/transport-client-throwing-npe-when-closing/176648/6 "2019-04-15T07:29:05Z")

</div>

You should call

```
client.close();
```

---

<div class="post-metadata">

**Author:** ![Chris\_Gatihi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_gatihi/32/25820_2.png) [@Chris\_Gatihi](https://discuss.elastic.co/u/Chris_Gatihi)\
**Post date:** [April 15, 2019, 1:14pm UTC](https://discuss.elastic.co/t/transport-client-throwing-npe-when-closing/176648/7 "2019-04-15T13:14:31Z")

</div>

Actually, that is what I'm calling. Not doing a static call to `TransportClient.close()`. Just wanted to be clear what class I was making that call against. I have a variable `client` of type `TransportClient` that I'm calling close on as you've suggested: `client.close()`.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 15, 2019, 1:40pm UTC](https://discuss.elastic.co/t/transport-client-throwing-npe-when-closing/176648/8 "2019-04-15T13:40:15Z")

</div>

Any chance that 2 threads are trying to close the same client at the same time?

---

<div class="post-metadata">

**Author:** ![Chris\_Gatihi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chris_gatihi/32/25820_2.png) [@Chris\_Gatihi](https://discuss.elastic.co/u/Chris_Gatihi)\
**Post date:** [April 15, 2019, 2:46pm UTC](https://discuss.elastic.co/t/transport-client-throwing-npe-when-closing/176648/9 "2019-04-15T14:46:52Z")

</div>

Actually, that thought had crossed my mind (one tries to close it after another has already done so results in that "thread was not started" illegal state) and, now that you mention it, I'm pretty sure that's what's happening ☹

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 13, 2019, 2:46pm UTC](https://discuss.elastic.co/t/transport-client-throwing-npe-when-closing/176648/10 "2019-05-13T14:46:56Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
