Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

IBar - TypeInitialization Problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    IBar - TypeInitialization Problem

    I've wasted the morning trying to fix this weird issue related to implementing the NinjaTrader.Core.Data.IBar type for my unit testing. I've attached a very simple VS project.

    I've stripped it back to a basic concrete class and a two line test method and I'm at a loss to explain the error.

    public class MyBar : IBar
    {
    public double Close { get; }
    public bool FirstBarOfSession { get; }
    public double High { get; }
    public double Low { get; }
    public double Open { get; }
    public DateTime Time { get; }
    public long Volume { get; }
    }
    [TestMethod]
    public void TestMethod1()
    {
    IBar bar = new MyBar();
    var close = bar.Close;
    }


    Error:

    Test method IBarTypeInitProblem.Test.TestMethod1 threw exception:

    System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> System.ArgumentNullException: Value cannot be null.

    Parameter name: ptr
    at System.Runtime.InteropServices.Marshal.GetDelegate ForFunctionPointer(IntPtr*ptr,*Type*t)
    at <CliSecureRT>.InitializeThroughDelegate(IntPtr)
    at <CliSecureRT>.Initialize()
    at .cctor()
    --- End of inner exception stack trace ---
    at IBarTypeInitProblem.Test.TestMethod1()



    remove the var close = bar.Close line and no error. Add it and it throws an exception before then. Any Ideas??

    #2
    Hello reach4thelasers,

    Thank you for your post.

    Can you call any other values? Such as bar.High or bar.Time?
    Or do they return the same exception?

    You can also look into using Try-Catch blocks to hopefully further isolate the matter: http://ninjatrader.com/support/forum...ead.php?t=9825

    Comment


      #3
      I use try catch blocks throughout my code - they didn't catch this. If I pass an IBar into the constructor of another class, the exception fires before the class constructor is entered, but only if I reference one of the properties defined in the IBar interface. I've been programming .NET C# for years but this has got me stumped.

      Comment


        #4
        Hello reach4thelasers,

        Thank you for your response.

        Do you have a script I could test this with?
        Last edited by NinjaTrader_PatrickH; 09-03-2015, 09:54 AM.

        Comment


          #5
          sorry forgot to attach... small project that shows the issue
          Attached Files

          Comment


            #6
            Hello reach4thelasers,

            Thank you for your response.

            There is no bar to pull the objects from and you are not returning anything for get.

            The error you get I do not. However, I do receive errors. These errors indicate that there is not object being pulled and thus no information for your test method.

            Comment


              #7
              The point of an interface is that I can make my own implementation of a bar "MyBar" that looks like a bar and acts like a bar but doesn't require me to be connected to the Ninjatrader application or servers.... It means that I can write code for consuming a bar without needing to connect and pull live data

              Simple types like double, int and bool are value types and default to zero if not defined. Properties declared as {get; } without a backing field are automatic properties - they can still be set from the constructor of the class - which is what I was doing before I stripped it back, never the less they should be returning zero.

              Strange that you don't get the error.... doesn't matter I went with my own type Candle/ICandle which lets me add more functionality anyway.

              Thanks for looking at it.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Jon17, Today, 04:33 PM
              0 responses
              1 view
              0 likes
              Last Post Jon17
              by Jon17
               
              Started by Javierw.ok, Today, 04:12 PM
              0 responses
              6 views
              0 likes
              Last Post Javierw.ok  
              Started by timmbbo, Today, 08:59 AM
              2 responses
              10 views
              0 likes
              Last Post bltdavid  
              Started by alifarahani, Today, 09:40 AM
              6 responses
              41 views
              0 likes
              Last Post alifarahani  
              Started by Waxavi, Today, 02:10 AM
              1 response
              21 views
              0 likes
              Last Post NinjaTrader_LuisH  
              Working...
              X