简体   繁体   English

toSeq.toDS() 给出 java.lang.NullPointerException

[英]toSeq.toDS() giving java.lang.NullPointerException

Getting nullpointer exception at w.toSeq.toDS() in the below code.在以下代码中的w.toSeq.toDS()处获取空指针异常。

var w = new ListBuffer[String]()
jList match {
  case Some(x) => {
    for (record <- x) {
      w += mapper.writeValueAsString(record)
    }
  }
  case None => println(s"No data read from file : ${f}")
}

import spark.implicits._
val jsonDataSet = w.toSeq.toDS()

Any idea what's wrong here?知道这里有什么问题吗?

It looks like it's not issue with toSeq.toDS() .看起来toSeq.toDS()没有问题。 The piece code which you shared should work without any error/exception.您共享的代码应该可以正常工作而不会出现任何错误/异常。 I have prepared test data as below and able to run successfully.我准备了如下测试数据并且能够成功运行。 Please find sample code as below.请找到如下示例代码。 It should be issue with some other piece of code.它应该是其他一些代码的问题。 I would request you to share entire code.我会要求你分享整个代码。

import org.apache.spark.sql.SparkSession
import scala.collection.mutable.ListBuffer

object TestToSeqToDs extends App {

  val spark = SparkSession
    .builder()
    .master("local")
    .appName("Test toSeq.toDS() function")
    .getOrCreate()

  def jList(): Option[Seq[String]] = {
    try {
      Some(Seq(("john"), ("Edward")))
    } catch {
      case e: Exception => None
    }
  }

  var w = new ListBuffer[String]()

  jList match {
    case Some(x) => {
      for (record <- x) {
        w += record
      }
    }
    case None => println("That didn't work.")
  }

  import spark.implicits._

  val jsonDataSet = w.toSeq.toDS()
   //OR
  val jsonDataSet = w.toDS()

  println(jsonDataSet.show())

+------+
| value|
+------+
|  john|
|Edward|
+------+

}

Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM