Immutable constructor argument order issues result in NullReferenceException
See original GitHub issueFor example:
class FooOptions
{
public FooOptions(string option, Uri foo)
{
Foo = foo;
Option = option;
}
[Option("foo")]
public Uri Foo { get; }
[Option("option")]
public string Option { get; }
}
Will result in:
nhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at CommandLine.Infrastructure.ReflectionHelper.CreateDefaultImmutableInstance(Type type, Type[] constructorTypes)
at CommandLine.Core.ReflectionExtensions.AutoDefault(Type type)
at CommandLine.Core.InstanceChooser.<>c__DisplayClass1_0.<MatchVerb>b__1()
at CommandLine.Core.InstanceBuilder.<>c__0`1.<Build>b__0_0(Func`1 f)
at CSharpx.MaybeExtensions.MapValueOrDefault[T1,T2](Maybe`1 maybe, Func`2 func, T2 noneValue)
at CommandLine.Core.InstanceBuilder.Build[T](Maybe`1 factory, Func`3 tokenizer, IEnumerable`1 arguments, StringComparer nameComparer, Boolean ignoreValueCase, CultureInfo parsingCulture, IEnumerable`1 nonFatalErrors)
at CommandLine.Core.InstanceChooser.MatchVerb(Func`3 tokenizer, IEnumerable`1 verbs, IEnumerable`1 arguments, StringComparer nameComparer, CultureInfo parsingCulture, IEnumerable`1 nonFatalErrors)
at CommandLine.Core.InstanceChooser.<>c__DisplayClass0_0.<Choose>b__0()
at CommandLine.Core.InstanceChooser.Choose(Func`3 tokenizer, IEnumerable`1 types, IEnumerable`1 arguments, StringComparer nameComparer, CultureInfo parsingCulture, IEnumerable`1 nonFatalErrors)
at CommandLine.Parser.ParseArguments(IEnumerable`1 args, Type[] types)
at CommandLine.ParserExtensions.ParseArguments[T1,T2](Parser parser, IEnumerable`1 args)
at CommandLineParserTest.Program.Main()
The ideal solution IMO would be to find the right ctor by param names (rather than order). But even if that’s not implemented, a better and more meaningful exception would be very helpful in debugging such issues.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Avoiding side effects in immutable class constructor
Josh's Builder gives you an immutable result but imposes no order on the methods called. The DSL can give you an immutable result...
Read more >Deserialize immutable classes with interface parameter in ...
I want to create some immutable classes with Json.net, but encountered error message: "Could not create an instance of type SolutionName.
Read more >NullReferenceException Class (System)
To address this problem, instantiate the object so that its value is no longer null . The following example does this by calling...
Read more >Immutables in Java - Are Setters Allowed?
An immutable object may have fields that are optional so that their value is null. Passing null into a constructor is a code...
Read more >How To Create an Immutable Class in Java
The FinalClassExample class defines the fields and provides the constructor method that uses deep copy to initialize the object.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
Top GitHub Comments
Agreed. BTW, this can get even more confusing when option class inheritance is in play. Spoiler: the inherited options (from the base options class) are expected at the end of the constructor parameter list.
@moh-hassan I would add some text like:
Otherwise a client may not understand why no matching constructor was found…