question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Immutable constructor argument order issues result in NullReferenceException

See original GitHub issue

For 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:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
ohadschncommented, Nov 11, 2017

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.

2reactions
ohadschncommented, Sep 11, 2019

@moh-hassan I would add some text like:

Note that constructor argument order must match property definition order in the options class (inherited options are expected at the end of the constructor parameter list).

Otherwise a client may not understand why no matching constructor was found…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found