Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

list

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!

like image 982
Rubbic Avatar asked Nov 17 '25 18:11

Rubbic


1 Answers

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

like image 173
ka4eli Avatar answered Nov 20 '25 07:11

ka4eli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!