我有以下代码部分,它给了我java.lang.NullPointerException
,我找到了源代码,我知道我声明了一个变量,但最初将其设置为空,后来在程序中初始化了它但我不知道如何给出默认值而不出错! List 接受两种不同的类型:Float
和 RDD
。
这是有问题的代码部分:
case class RPN (sc:SparkContext, vp: VolumeProperty, var stack:List[Either[RDD[(Int, Array[Float])],Float]]) {
def this(s:SparkContext,v:VolumeProperty) = this(s,v,null); //Think here is the problem
def operand(x: Either[RDD[(Int, Array[Float])],Float]) = new RPN(sc,vp,stack = x :: stack) //gives error on this line
我收到以下错误:
Exception in thread "main" java.lang.NullPointerException
如何解决!
最佳答案
使用Nil
而不是null
。 Nil
是一个空列表。
关于java - 如何为 Scala 中的列表赋予默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31466862/