Noy Noy - 1 year ago 114
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)

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.

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(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at com.intellij.rt.execution.application.AppMain.main(

Noy Noy
Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download