scala - getting java.util.NoSuchElementException
<p>getting java.util.NoSuchElementException</p>

<pre><code>val data3 = data2.filter(x =&gt; type1_dm_med.contains(x._2._1.last.medicine))
</code></pre>

<p>when I try to <code>print data3.count</code> I get below error - not sure how to fix it, please help</p>

<pre><code>[Stage 50:=====&gt; (20 + 1) / 200]16/02/26 01:37:11 ERROR Executor: Exception in task 22.0 in stage 50.0 (TID 1636)
java.util.NoSuchElementException
at scala.collection.LinearSeqOptimized$class.last(LinearSeqOptimized.scala:135)
at scala.collection.immutable.List.last(List.scala:84)
</code></pre>

<p>more context:</p>

<pre><code> class Diagnostic(patientID:String, date: Date, code: String)
 class Medication(patientID: String, date: Date, medicine: String)
</code></pre>

<p>loaded data from .csv into medication:RDD[Medication] and diagnostic:<br>
 RDD[Diagnostic]</p>

<pre><code>val mpairs = medication.map(x=&gt;(x.patientID,x))
val dpairs = diagnostic.map(x=&gt;(x.patientID,x))
val data = mpairs.fullOuterJoin(dpairs)
</code></pre>

<p>'data' tuple example:
(000496120-01,(Some(Medication(000496120-01,Fri Jul 01 15:15:00 EDT 2005,protamine sulfate)),Some(Diagnostic(000496120-01,Mon Jan 07 15:00:00 EST 2013,v70.0))))</p>

<p>type1_dm_dx et'al are list of code or medicines that want to filter on</p>

<pre><code>val cond1 = data.filter(x =&gt; type1_dm_dx.contains(x._2._2.last.code))
val data1 = data.subtractByKey(cond1)
val data2 = data1.filter(x =&gt; type2_dm_dx.contains(x._2._2.last.code))
val data3 = data2.filter(x =&gt; type1_dm_med.contains(x._2._1.last.medicine))
</code></pre>

<p>hope this helps</p>