Results 1 to 4 of 4

Thread: Spring.Expressions -> unhandled exception while using Lambda expression.

  1. #1
    Join Date
    Jan 2011
    Posts
    2

    Default Spring.Expressions -> unhandled exception while using Lambda expression.

    Hi,

    I am new to Spring.NET and I am evaluating Spring.NET for several of my projects and running into an issue with the Spring.Expressions library with regard to the Lambda Expressions discussed in section 11.3.15 of the manual. I am on the latest version 1.3.1 of Spring.NET.

    I am trying the following from page #117 of the manual as is which is not working:

    IDictionary vars = new Hashtable();
    Expression.RegisterFunction("sqrt", "{|n| Math.Sqrt($n)}", vars);

    ExpressionEvaluator.GetValue(null, "#sqrt(9)", vars) // 3


    After successful compilation when I run my application, I get the following runtime error on Math.Sqrt call:

    An unhandled exception of type 'Spring.Core.NullValueInNestedPathException' occurred in Spring.Core.dll

    Additional information: Cannot initialize property or field node 'Math' because the specified context is null.


    My guess is that Spring.Core is not able to find the .NET System.Math library.

    What am I doing wrong here and how can I fix this issue?

    Thanks,
    Faisal

  2. #2
    Join Date
    Jan 2011
    Location
    New York, NY
    Posts
    6

    Default

    This is actually a very simple fix: the problem derives from the fact that the Spring Expression Language (SpEL) needs to be 'told' how to resolve the string "Math" when it is encountered in a statement.

    See Section 11.3.8, Type Registration for more info.

    Thus, if you change your code to include the requisite type registration code, it should work for you as in...

    Code:
    TypeRegistry.RegisterType("Math", typeof(Math));
    
    IDictionary vars = new Hashtable();
    Expression.RegisterFunction("sqrt", "{|n| Math.Sqrt($n)}", vars);
    
    var value = ExpressionEvaluator.GetValue(null, "#sqrt(9)", vars);
    Unfortunately, the snippet in the section of the docs which you reference wasn't (entirely) intended to be a complete, runnable, stand-alone code block and so as-is it will definitely evidence the issue that you point out (e.g., not understanding what to do when it encounters the string "Math" in the SpEL statement).

    Hope this helps clarify what's going on here.

    Also, as an aside, I apologize for the delay in responding to this post, but this forum is generally reserved for Spring Java discussions and I only just recently was made aware of this post. In the future, for questions re: Spring.NET, we recommend that you post your questions in the Spring.NET-specific discussion forums so that they will (hopefully!) receive a speedier response.

    Thanks,

    -Steve B.

  3. #3
    Join Date
    Jan 2011
    Posts
    2

    Default

    Hi Steve,

    Thank you very much. That fixed my issue. I am really new to this so I apologize for the trivial question. Whatever I have seen in Spring.NET so far is truly great and top notch.

    I will remember to post on the correct Spring.NET forum from hereon.

    Thanks again,
    Faisal Ali

  4. #4
    Join Date
    Jan 2011
    Location
    New York, NY
    Posts
    6

    Default ~!

    No troubles; the forums are here to support both the simple and the complex questions so feel free to ask as many questions as you want

    Glad that solved it for you~!

    -Steve B.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •