two useful tips or how-to avoid 'first chance exception of type 'System.NullReferenceException' occurred in Jint.dll'

Aug 16, 2011 at 7:16 AM
Edited Aug 16, 2011 at 7:18 AM

Well, I have spent the whole day to figure out why my scripts suddenly stop working. (v 0.9.0).

I nearly gave-up and decided to switch to the Microsoft JScript.NET when I found the source of the problem.

 

Here is the code of my testing console application that reproduces the problem.

 

namespace Tests
{
    class MyClass
    {
        public void Print(string s)
        {
            Debug.Print(s);
        }
    }

    class Program
    {
        static string script = @"function MyFunction(a) {		 	

                                    //a.Print('some text');

                                };";

        static void Test1()
        {
            MyClass obj = new MyClass();

            JintEngine engine = new JintEngine();

            var res = engine.Run(script);

            var res2 = engine.CallFunction("MyFunction", obj);
        }

        static void Main(string[] args)
        {
            try
            {
                Test1();
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
                
                if (e.InnerException != null)
                    Debug.Print(e.InnerException.Message);
            }
        }
    }
}

 

 

While running this application you will receive two errors;

1. Jint.Marshaller.MarshalJsValue(Jint.Native.JsInstance)

2.  A first chance exception of type 'System.NullReferenceException' occurred in Jint.dll

 

And here is the corrected version of the same project:

 

 

namespace Tests
{
    public class MyClass
    {
        public void Print(string s)
        {
            Debug.Print(s);
        }
    }

    class Program
    {
        static string script = @"function MyFunction(a) {		 	

                                    //a.Print('some text');
                                    
                                    return true;
                                };";

        static void Test1()
        {
            MyClass obj = new MyClass();

            JintEngine engine = new JintEngine();

            var res = engine.Run(script);

            var res2 = engine.CallFunction("MyFunction", obj);
        }

        static void Main(string[] args)
        {
            try
            {
                Test1();
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
                
                if (e.InnerException != null)
                    Debug.Print(e.InnerException.Message);
            }
        }
    }
}

 

So here are my tips:

1. Your JavaScript functions must always return a value.

2. Make sure that classes that you want pass to script are marked 'public'.

 

Thanks,

Khachatur

http://www.httpdebugger.com

Coordinator
Aug 16, 2011 at 5:17 PM

Actually javascript functions return 'undefined' if there is no 'return' statement. I'll take some time to see how it should behave.

Aug 16, 2011 at 7:07 PM

yes, but I receive 'System.NullReferenceException' exception if there is no return statement.

 

Thanks,

Khachatur

http://www.httpdebugger.com

Coordinator
Aug 17, 2011 at 10:33 PM

I have pushed some bug fixed on the dev branch, you might have more success with it.

Aug 19, 2011 at 2:48 PM

Hello Sebastien, 

Thank you for providing the bug-fix.

I have  tested the new version and it looks like that it is OK now.

I will do more deep testing soon and send you results.

 

Thanks,

Khachatur,

http://www.httpdebugger.com