java.lang.RuntimeException when using client in Java API in Kotlin

I am trying to use the Elasticsearch Java API in a Kotlin application, following the official tutorial page (Connecting | Elasticsearch Java API Client [8.6] | Elastic). However, I am encountering a java.lang.RuntimeException when trying to perform basic actions such as or client.index(). I am not sure what is causing the error.

Here is an example from my test application:

val restClient = RestClient.builder(
                HttpHost("localhost", 9200)

            val transport: ElasticsearchTransport = RestClientTransport(
                restClient, JacksonJsonpMapper()
            val client = ElasticsearchClient(transport)
            di("client ${client.toString()}")

            // create a StringReader object containing a JSON-formatted log message
            val input: Reader = StringReader(
                "{'@timestamp': '2022-04-08T13:55:32Z', 'level': 'warn', 'message': 'Some log message'}"
                    .replace('\'', '"')
            // create an IndexRequest with an index named "logs" and the JSON-formatted log message
            // wrapped in a JsonData object
            val request = IndexRequest.of(
                Function { i: IndexRequest.Builder<JsonData?> ->
            // execute the index request using the Elasticsearch client and store the response
            val response: IndexResponse = client.index(request)

            // log the version of the newly indexed document
            di("Indexed with version " + response.version())

java.lang.RuntimeException doesn't carry a lot of information to understand what's happening. Can you provide the exception message and stack trace?

here is the complete message error I get from android studio using debug mode:

023-03-05 19:41:55.425 9155-9155/com.example.etax E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.etax, PID: 9155
        at org.elasticsearch.client.RestClient.extractAndWrapCause(
        at org.elasticsearch.client.RestClient.performRequest(
        at org.elasticsearch.client.RestClient.performRequest(
        at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(
        at co.elastic.clients.elasticsearch.ElasticsearchClient.index(
        at com.example.etax.MainActivity.sendFileStandard(MainActivity.kt:96)
        at com.example.etax.MainActivity.onCreate$lambda-0(MainActivity.kt:39)
        at com.example.etax.MainActivity.$r8$lambda$WxtDuuQ6iwM7qx_huJKFXiJu8fI(Unknown Source:0)
        at com.example.etax.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
        at android.view.View.performClick(
        at android.view.View.performClickInternal(
        at android.view.View.access$3900(
        at android.view.View$
        at android.os.Handler.handleCallback(
        at android.os.Handler.dispatchMessage(
        at android.os.Looper.loop(
        at java.lang.reflect.Method.invoke(Native Method)
     Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(
        at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(
        at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(
        at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(
        at org.apache.http.nio.pool.AbstractNIOConnPool.processPendingRequest(
        at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.requestConnection(
        at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.requestConnection(
        at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.start(
        at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase.execute(
        at org.apache.http.impl.nio.client.InternalHttpAsyncClient.execute(
        at org.elasticsearch.client.RestClient.performRequest(

Caused by: android.os.NetworkOnMainThreadException

On Android making network calls on the main thread that handles the UI is forbidden as it can make the application unresponsive (see docs).

You should do these calls from another thread, or use the async version RestClient.performRequestAsync().

Thank you for your help, really appreciated. Would you please link me the official guide/info page about the asynchronous call so I can start to work on it?

Unfortunately I get onFailure: java.util.concurrent.CancellationException: Request execution cancelled or if i delete the restclient.close() instruction my app just block (I suppose waiting for the request to be performed).
I would like to add that while i performed this operations both elasticsearch and kibana was running on my pc
So i add a waiting instruction and i get a refused error

private fun sendFileStandard() {
    val restClient = RestClient.builder(
        HttpHost("localhost", 9200, "http"),
        HttpHost("localhost", 9201, "http")

    val request = Request(

    var risultato = "default value"

    doAsync {
        val cancellable: Cancellable = restClient.performRequestAsync(request,
            object : ResponseListener {
                override fun onSuccess(response: Response) {
                    risultato = "success" + response.toString()

                override fun onFailure(exception: Exception) {
                    risultato = "exception" + exception.toString()
    println("risultato: $risultato")

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