Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException with Array creation?

Scala has a weird drawback, that I cannot create a default argument in args. Here is my latest attempt:

object Main extends java.lang.Object with ScalaObject with App {
    override val args:Array[String]=Array(args.toList.headOption.getOrElse("f"))
    println("args(0) = " + args(0))
}

Run the code here: http://ideone.com/B20HBA

Exception in thread "main" java.lang.NullPointerException at scala.collection.mutable.ArrayOps$ofRef$.length$extension(ArrayOps.scala:114) at scala.collection.mutable.ArrayOps$ofRef.length(ArrayOps.scala:114) at scala.collection.SeqLike$class.size(SeqLike.scala:106) at scala.collection.mutable.ArrayOps$ofRef.size(ArrayOps.scala:108) at scala.collection.mutable.Builder$class.sizeHint(Builder.scala:69) at scala.collection.mutable.ListBuffer.sizeHint(ListBuffer.scala:45) at scala.collection.TraversableLike$class.to(TraversableLike.scala:628) at scala.collection.mutable.ArrayOps$ofRef.to(ArrayOps.scala:108) at scala.collection.TraversableOnce$class.toList(TraversableOnce.scala:257) at scala.collection.mutable.ArrayOps$ofRef.toList(ArrayOps.scala:108) at Main$delayedInit$body.apply(Main.scala:2) at scala.Function0$class.apply$mcV$sp(Function0.scala:40) at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12) at scala.App$$anonfun$main$1.apply(App.scala:71) at scala.App$$anonfun$main$1.apply(App.scala:71) at scala.collection.immutable.List.foreach(List.scala:318) at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32) at scala.App$class.main(App.scala:71) at Main$.main(Main.scala:1) at Main.main(Main.scala)

like image 492
A T Avatar asked Dec 05 '25 13:12

A T


1 Answers

As @blast_hardcheese has shown, you cannot usefully set the value of a val to an expression dependent on itself, which is what is happening in your code above.

If you really need to do this (I can't think of a reason why you would), you can reference the original value by appending super. in front of the reference to the 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 that your original expression would lose any provided arguments beyond the first. That may have been your intention, but in case it wasn't, you would want to change it to something like the above.

like image 152
Shadowlands Avatar answered Dec 08 '25 19:12

Shadowlands



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!