Skip to content
pdemarti edited this page May 6, 2015 · 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:

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

or

trait Outer extends FieldArgs {
  var stuff: Inner = new Inner{}
}

Runtime Exception in thread "main" com.quantifind.sumac.ArgException: unknown option xyz

When using a joda.DateTime argument, you must register a DateTime parser. Otherwise Sumac will skip that argument (as it does for any argument it doesn't know how to handle). The result is that you'll get a runtime exception when trying to pass the argument on the command line (but not otherwise). Consider the following code:

\\ BAD: missing custom parser
trait MyArgs extends FieldArgs {
  var start: DateTime = new DateTime("2015-04-01")
}

If you run a program that uses MyArgs without specifying --start, things will work as expected with the default value specified. But if you do specify --start 2015-04-18 or any other date/time, then you'll get:

Exception in thread "main" com.quantifind.sumac.ArgException: unknown option start

So the correct code should be:

trait MyArgs extends FieldArgs {
  registerParser(USDateTimeParser)
  var start: DateTime = new DateTime("2015-04-01")
}

And remember that:

  1. conforming with Best Practices, it is advisable to define your own base trait, register the custom parser there, and use that trait everywhere instead of using FieldArgs directly.

  2. add sumac-ext (e.g.: "com.quantifind" %% "sumac-ext" % "0.3.0") to your dependencies.