title: How to resolve java.lang.NullPointerException in scala?
body: <p>assume part of my code is like as:- 
  where doc is List[Document] that contains stu_name and roll_number
  sometimes stu_name and roll_name may be null. </p>
  
  <p>I used Try to avoid null Pointer exception in first two lines.
  but why I m getting again Null Pointer exception in "val myRow".</p>
  
  <pre><code>    val name= Try {Option.apply(doc.getFieldValue("stu_name"))}.getOrElse(null)
      val rollNumber ={Option.apply(doc.getFieldValue("roll_number"))}.getOrElse(null)
  
      val myRow = (
       doc.getFieldValue("ID").asInstanceOf[Int]   //can't be null
       name.getOrElse(null).toString,   //NullPointerException
       rollNumber.getOrElse(null).asInstanceOf[Int] //NullPointerException
      )
  
      .....
      .....
  </code></pre>
  
  <p>I m getting following error:</p>
  
  <pre><code> [2016-01-14 22:40:16,896] WARN  o.a.s.s.TaskSetManager [] [akka://JobServer/user/context-supervisor/demeter] - Lost task 0.0 in stage 0.0 (TID 0, 10.29.23.136): java.lang.NullPointerException
  at com.test.events.Monitoring$$anonfun$geteventTableReplicateDayFunc$1.apply(Monitoring.scala:75)
  at com.test.events.Monitoring$$anonfun$geteventTableReplicateDayFunc$1.apply(Monitoring.scala:57)
  at com.test.events.Monitoring$$anonfun$27.apply(Monitoring.scala:104)
  at com.test.events.Monitoring$$anonfun$27.apply(Monitoring.scala:104)
  </code></pre>
  
  <p>I tried in console following but did not see any error:</p>
  
  <pre><code>scala&gt; val a = Try (Option.apply("atar")).getOrElse(null)
  a: Option[String] = Some(atar)
  scala&gt; a.getOrElse(null)
  res16: String = atar
  
  scala&gt; val a = Try (Option.apply(null)).getOrElse(null)
  a: Option[Null] = None
  scala&gt; a.getOrElse(null)
  res17: Null = null
  </code></pre>