Skip to content
imranr edited this page Dec 27, 2013 · 4 revisions
  • java.lang.InstantiationException with nested args

When using nested args, you can run into InstantiationExceptions if you nest an uninitialized trait. Eg., given this definition:

trait Inner extends FieldArgs {
  var a = 0
}
trait Outer extends FieldsArgs {
  var stuff: Inner = _
}

You will get an InstantiationException when you try to use Inner. The reason is that stuff is initialized to null, and so Sumac is trying to create an instance of Inner to use, but that leads to an InstantiationException. Instead, you can either change the type of stuff to a concrete class, or initialize it with an anonymous class. Eg:

trait InnerImpl extends Inner
trait Outer extends FieldArgs {
  var stuff: InnerImpl = _
}

or

trait Outer extends FieldArgs {
  var stuff: Inner = new Inner{}
}
Clone this wiki locally