Mahsa
Mahsa

Reputation: 1550

How to give a default value to a List in Scala?

I have the following part of code which gives me java.lang.NullPointerException, I found the source and I know that I declared a variable but set it initially null and later in the program initialized it but I don't know how to give a default value without getting error! The List accepts two different types, Float and RDD. Here is the part of the code that has problem in it:

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

and I am getting following error:

Exception in thread "main" java.lang.NullPointerException

How can I solve it!

Upvotes: 3

Views: 1813

Answers (1)

ka4eli
ka4eli

Reputation: 5424

Use Nil instead of null. Nil is an empty list.

Upvotes: 7

Related Questions