scala - NullPointerException with Array creation? -
scala has weird drawback, cannot create default argument in args
. here latest attempt:
object main extends java.lang.object scalaobject app { override val args:array[string]=array(args.tolist.headoption.getorelse("f")) println("args(0) = " + args(0)) }
run code here: http://ideone.com/b20hba
exception in thread "main" java.lang.nullpointerexception @ scala.collection.mutable.arrayops$ofref$.length$extension(arrayops.scala:114) @ scala.collection.mutable.arrayops$ofref.length(arrayops.scala:114) @ scala.collection.seqlike$class.size(seqlike.scala:106) @ scala.collection.mutable.arrayops$ofref.size(arrayops.scala:108) @ scala.collection.mutable.builder$class.sizehint(builder.scala:69) @ scala.collection.mutable.listbuffer.sizehint(listbuffer.scala:45) @ scala.collection.traversablelike$class.to(traversablelike.scala:628) @ scala.collection.mutable.arrayops$ofref.to(arrayops.scala:108) @ scala.collection.traversableonce$class.tolist(traversableonce.scala:257) @ scala.collection.mutable.arrayops$ofref.tolist(arrayops.scala:108) @ main$delayedinit$body.apply(main.scala:2) @ scala.function0$class.apply$mcv$sp(function0.scala:40) @ scala.runtime.abstractfunction0.apply$mcv$sp(abstractfunction0.scala:12) @ scala.app$$anonfun$main$1.apply(app.scala:71) @ scala.app$$anonfun$main$1.apply(app.scala:71) @ scala.collection.immutable.list.foreach(list.scala:318) @ scala.collection.generic.traversableforwarder$class.foreach(traversableforwarder.scala:32) @ scala.app$class.main(app.scala:71) @ main$.main(main.scala:1) @ main.main(main.scala)
as @blast_hardcheese has shown, cannot usefully set value of val expression dependent on itself, happening in code above.
if need (i can't think of reason why would), can reference original value appending super.
in front of reference original value:
object main extends app { override val args: array[string] = if (super.args.isempty) array("f") else super.args println("args(0) = " + args(0)) }
note original expression lose provided arguments beyond first. may have been intention, in case wasn't, want change above.
Comments
Post a Comment