Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.NullReferenceException: Object reference not set to an instance of an object. #252

Closed
donaldnevermore opened this issue Sep 1, 2021 · 2 comments

Comments

@donaldnevermore
Copy link

donaldnevermore commented Sep 1, 2021

OS: Windows 10
dotnet version: 5.0.400

Step to reproduce: run the sample code.

using System;
using CSScriptLib;

var calc = CSScript.Evaluator
    .LoadCode<ICalc>(@"using System;
                                    public class Script
                                    {
                                        public int Sum(int a, int b)
                                        {
                                            return a+b;
                                        }
                                    }");
var result = calc.Sum(1, 2);
Console.WriteLine(result);

public interface ICalc {
    int Sum(int a, int b);
}

Output:

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at CSScriptLib.EvaluatorBase`1.LoadCode[T](String scriptText, Object[] args)
   at <Program>$.<Main>$(String[] args) in E:\Monorepos\test-driven-design\Main.cs:line 4
@oleg-shilo
Copy link
Owner

Hi Donald,

There is a critical mistake in the API documentation. It states that LoadCode<T> returns aligned to interface object (duck-typed):
image

This is true only for the LoadMethod<T> but not for LoadCode<T> returns a canonical typecasted object. This is due to the fact that LoadMethod decorates it with the class definition so it can inject inheritance statement but LoadCode processes the code as-is and cannot inject anything.

You can fix your code by moving to LoadMethod:

var calc = CSScript.Evaluator
                   .LoadMethod<ICalc>(@"using System;
                                        public int Sum(int a, int b)
                                        {
                                            return a+b;
                                        }");
var result = calc.Sum(1, 2);
Console.WriteLine(result);

or explicitly implementing the interface:

var calc = CSScript.Evaluator
                   .LoadCode<ICalc>(@"using System;
                                      public class Script : ICalc
                                      {
                                          public int Sum(int a, int b)
                                          {
                                              return a+b;
                                          }
                                      }");
var result = calc.Sum(1, 2);
Console.WriteLine(result);

oleg-shilo pushed a commit that referenced this issue Sep 2, 2021
…o an instance of an object. (updated API doc)
@oleg-shilo
Copy link
Owner

The documentation is fixed now and will be available with the very next release

oleg-shilo pushed a commit that referenced this issue Sep 12, 2021
- Issue #252: System.NullReferenceException: Object reference not set to an instance of an object. (updated API doc)
oleg-shilo pushed a commit that referenced this issue Sep 12, 2021
- Issue #252: System.NullReferenceException: Object reference not set to an instance of an object. (updated API doc)
- Issue #253: Supports both .Net Framework and .Net 5
oleg-shilo pushed a commit that referenced this issue Sep 26, 2021
- Issue #254: Script merger for hosted scripts
- Issue #252: System.NullReferenceException: Object reference not set to an instance of an object. (updated API doc)
- Issue #253: Supports both .Net Framework and .Net 5
oleg-shilo pushed a commit that referenced this issue Nov 4, 2021
- Updated `-speed` and `-code` with the complete support `-ng:*` switches
- Added `IEvaluator.IsCachingEnabled`. Ite as always available from the conctrete types implementing `IEvaluator` and now it is moved directly to the interface.
- Added `-servers:start` and `-servers:stop` command to control both Roslyn and csc build servers at the same time
- CSScriptLib: Native API `CacheEnabled` marked as obsolete
- Issue #258: Can not run scripts after installing VS2022
- Issue #257: Ability to catch AppDomain.UnhandledException in a not-hosted script (cscs)
- Issue #255: Relative path for cscs.exe -out option results in wrong output folder
- Issue #254: Script merger for hosted scripts
- Issue #253: Supports both .Net Framework and .Net 5
- Issue #252: System.NullReferenceException: Object reference not set to an instance of an object. (updated API doc)
- Added auto-generation of the CLI MD documentation with `-help cli:md`. To be used to generate GitHub wiki page during the build
- Fixed Debian packaging problem (`/n/r` needed replacement with `\n`)
oleg-shilo added a commit that referenced this issue Nov 14, 2021
### Misc

- Added auto-generation of the CLI MD documentation with -help cli:md. To be used to generate GitHub wiki page during the build
- Fixed Debian packaging problem (/n/r needed replacement with \n)
- Issue #253: Supports both .Net Framework and .Net 5

### CLI

- Updated -speed and -code with the complete support -ng:* switches
- Added -servers:start and -servers:stop command to control both Roslyn and csc build servers at the same time
- Issue #258: Can not run scripts after installing VS2022
- Issue #257: Ability to catch AppDomain.UnhandledException in a not-hosted script (cscs)
- Issue #255: Relative path for cscs.exe -out option results in wrong output folder
- Issue #254: Script merger for hosted scripts
- Issue #252: System.NullReferenceException: Object reference not set to an instance of an object. (updated API doc)

### CSScriptLib

- Native API CacheEnabled marked as obsolete
- Added IEvaluator.IsCachingEnabled. It is always available from the concrete types implementing IEvaluator and now it is moved directly to the interface.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants