More Than Five
More Than Five

Reputation: 10419

Why does new fail?

In scala this is ok

val v = Vector(1,2,3)

This is not ok

val v = new Vector(1,2,3); 

You get:

java.lang.NullPointerException
                                                  //|   at scala.collection.immutable.VectorIterator.next(Vector.scala:660)
                                                  //|   at scala.collection.Iterator$$anon$10.next(Iterator.scala:312)
                                                  //|   at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
                                                  //|   at scala.collection.Iterator$class.foreach(Iterator.scala:727)
                                                  //|   at scala.collection.AbstractIterator.foreach(Iterator.scala:1157)
                                                  //|   at scala.

Why?

Upvotes: 1

Views: 167

Answers (1)

Zavior
Zavior

Reputation: 6452

Have a look at the api http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Vector

You are trying to invoke the constructor that looks like this:

new Vector(startIndex: Int, endIndex: Int, focus: Int)

with this:

val v = new Vector(1,2,3);

Looks like it is trying to reach the index at 3, but clearly the vector is not long enough

Upvotes: 6

Related Questions