Ayub Malik Ayub Malik - 4 months ago 12
Java Question

Elasticsearch local node client NPE when creating index in java test

I am using elasticsearch (v2.3.2/Windows 7) local node to create an index in a java test which is throwing an null pointer exception.

The test code is as follows:

CreateIndexResponse response = esClient.admin().indices().prepareCreate("mytestindex")
.setSettings(Settings.builder()
.put("number_of_replicas", 0)
.put("number_of_shards", 1))
.execute().actionGet();


I have verified esClient is created by spring and is not null. The code to create the client is:

@Bean
public Client localElasticsearchClient() {

Settings settings = Settings.settingsBuilder()
.put("path.home", "/tmp/elasticsearch")
.put("path.data", "/tmp/elasticsearch")
.build();

return NodeBuilder.nodeBuilder()
.local(true)
.settings(settings)
.build()
.client();

}


The stacktrace is as follows:

java.lang.NullPointerException
at org.elasticsearch.action.support.master.TransportMasterNodeAction$AsyncSingleAction.<init>(TransportMasterNodeAction.java:129)
at org.elasticsearch.action.support.master.TransportMasterNodeAction.doExecute(TransportMasterNodeAction.java:107)
at org.elasticsearch.action.support.master.TransportMasterNodeAction.doExecute(TransportMasterNodeAction.java:51)
at org.elasticsearch.action.support.TransportAction.execute(TransportAction.java:137)
at org.elasticsearch.action.support.TransportAction.execute(TransportAction.java:85)
at org.elasticsearch.client.node.NodeClient.doExecute(NodeClient.java:58)


I saw something here about it but there was no info on how to correct it.

Answer

Actually the answer was implicitly given in this link

I was not calling node.start() method i.e.

return NodeBuilder.nodeBuilder()
        .local(true)
        .settings(settings)
        .build()
        .start()
        .client();
Comments