Noy Noy - 1 month ago 8
Scala Question

ElasticClient giving NullPointerException

Using Scala 2.11.8 and a local instance of Elasticsearch 2.3.5 and the following dependencies:

libraryDependencies ++ = Seq(
"com.sksamuel.elastic4s" %% "elastic4s-core" % "2.3.1",
"org.apache.spark" %% "spark-core" % 1.6.2,
"org.apache.spark" %% "spark-sql" % 1.6.2,
"org.elasticsearch" % "elasticsearch" % 2.4.0,
"org.elasticsearch" %% "elasticsearch-spark" % 2.4.0
)


I get a NullPointerException when running the last line below:

val conf = new SparkConf().setAppName("DataIndexer").setMaster("local[*]")
val sc = new SparkContext(conf)
val sqlContext = new SQLContext(sc)
val uri = ElasticsearchClientUri("elasticsearch://localhost:9300")
val client = ElasticClient.transport(uri)
client.execute(indexExists(name)).await.isExists


I'm new to Scala/Spark and I haven't found anything regarding this error. I assume it's something obvious but I'm not sure what. Any help would be appreciated.

EDIT:
Here's the stack trace:

Exception in thread "main" java.lang.NullPointerException
at com.unleashbts.thor.ElasticIngestor$.ensureIndex(ElasticIngestor.scala:46)
at com.unleashbts.thor.ElasticIngestor$.ingest(ElasticIngestor.scala:59)
at com.unleashbts.thor.ElasticIngestor$.main(ElasticIngestor.scala:42)
at com.unleashbts.thor.ElasticIngestor.main(ElasticIngestor.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Noy Noy
Answer

It looks like because I was doing this in a separate class, I was getting this error (my sample code above wasn't accurate enough). I moved the code into the class where the execute command was being called, this solved my issue.