title: NullPointerException while reading a column from the row
body: <p>The following Scala (Spark 1.6) code for reading a value from a Row fails with a <code>NullPointerException</code> when the value is null.</p>
  
  <pre><code>val test = row.getAs[Int]("ColumnName").toString
  </code></pre>
  
  <p>while this works fine</p>
  
  <pre><code>val test1 = row.getAs[Int]("ColumnName") // returns 0 for null
  val test2 = test1.toString // converts to String fine
  </code></pre>
  
  <p>What is causing <code>NullPointerException</code> and what is the recommended way to handle such cases?</p>
  
  <p>PS: getting row from DataFrame as follows:</p>
  
  <pre><code>val myRDD = myDF.repartition(partitions)
    .mapPartitions{ rows =&gt; 
      rows.flatMap{ row =&gt;
        functionWithRows(row) //has above logic to read null column which fails
      }
    }
  </code></pre>
  
  <p><code>functionWithRows</code> has then above mentioned <code>NullPointerException</code>.</p>
  
  <p>MyDF schema:</p>
  
  <pre><code>root
   |-- LDID: string (nullable = true)
   |-- KTAG: string (nullable = true)
   |-- ColumnName: integer (nullable = true)
  </code></pre>