Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Object Error

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

    Object Error

    I'm comparing the current date to a specific date in the future. Went to the library & got this example:

    protected override void OnBarUpdate()
    {

    // Compare the date of the current bar to September 15, 2014

    if (ToDay(Time[0]) > 20140915)
    {
    // Do something
    }


    Plugging this in respective to task I got an object reference error "Object reference not set to an instance of an Object". As the library duly noted, these dayTime statements can be tricky for the less-informed ... Heads Up the links provided in the library went to dead microsoft pages so not sure how to set this instance.

    Thanx,
    JM

    #2
    Hello JM,

    Thank you for your post.

    The error message "Object reference not set to an instance of an Object" occurs when you try to reference a variable that has a null value. We have a tip page in the help guide about checking for null references here:


    Which date in the future are you comparing the current date to? Also, do you mean the current date at the time the script is enabled, or do you mean the date of the current bar being processed? Once I have a better understanding of what you are looking for I would be glad to provide a different snippet or example.

    Additionally, which page are you referring to where you found the example? Please provide a link to the library/help guide page in question so I may further investigate the links to dead Microsoft pages. It sounds like we need to get that rectified.

    I look forward to your reply.

    Comment


      #3
      Thank You.

      1) Dead Link: https://ninjatrader.com/support/help...tml?totime.htm the DateTime link

      2) I'm creating a future date kill switch bool.

      if (ToDay(Time[0]) < 20230630)
      {
      Order = true;
      }​

      I'm thinking I need a declaration up top here or something to that effect. Library insinuates this is a direct C# piece of logic or so I gleaned, so tried a direct placement. Currently code is residing in the OnStateChange dialog.

      Comment


        #4
        Hello johnMoss,

        Thank you for your reply.

        I appreciate you sharing the link. I will look into this and suggest help guide edits as necessary to get it corrected.

        OnStateChange() is called when your script enters a new state. If you want to reference the time of the current bar with Time[0] that should be done in OnBarUpdate(). Additionally, the use of "Order" for the name of your bool might cause errors because Order is a type of object in NinjaScript. You may need to use a bool like allowOrders instead.

        Please try moving this condition to OnBarUpdate() and using a different name for your bool. Otherwise, the way you have the condition set up as if (ToDay(Time[0]) < 20230630) should work as expected and likely isn't the issue. You may even use print statements to verify each value used in your conditions and actions to better understand how your script behaves. For more details on using prints to debug a script:Please let us know if we may be of further assistance.

        Comment


          #5
          Thank you and your welcome ... I already did a debug and have verified the above as the problem. The issue here is object reference not set to an instance of an object. Isn't that a declaration of sorts up in Defaults or public class maybe?

          Changing 'Order' is probably a good idea anyway in terms of better syntax but currently it works just fine. There actually are two arguments in the conditional statement and the other has checked good when i commented out the && (ToDay(Time[0]) < 20230630) logic. And this in it's current placement in OnStateChange. I do note that I do have the other object with a starting int value declared in defaults.

          Comment


            #6
            Hello johnMoss,

            Thank you for your reply.

            "Object reference not set to an instance of an object" is a general C# error so no, it is not necessarily specific to State.SetDefaults or something at the class level. It simply means you are referring to an object that does not exist. Time[0] does not exist in OnStateChange() because that method is not synced up to the time series that is able to sync up the time with a barsAgo index. As mentioned in the help guide, "In most cases, you will access the historical time series using a core event handler such as OnBarUpdate."


            In the snippet offered for ToDay(), the comparison for the condition is made in OnBarUpdate(). If you want to add a check of Time from the time series for a specific bar index, you will need to use it in a method like OnBarUpdate() or else it is expected that you would get an error if a value has not been assigned to that object yet or the object does not exist.

            Thank you for your patience.

            Comment


              #7
              OK, I'm closing in: Here is my current code, not sure where I'm messing up All else edited out here, just showing placements:


              public class indicator myIndicatortest : Indicator
              {
              private DateTime ResetOrderTime;
              private DateTime CurDate;

              protected override void OnStateChange()
              {
              if (State == State.SetDefaults)
              {
              ResetOrderTime = DateTime.Parse("2024-05-24T00:01:01");
              CurDate = GetDateTime();
              }
              }


              protected override void OnBarUpdate()
              {
              if (CurDate.Date <= ResetOrderTime.Date)
              {
              (I have a bool here. Check good)
              }
              }
              }



              Compile Error = The name GetDateTime does not exist in the current context…


              I tried moving "
              CurDate = GetDateTime();​ " into OnBarUpdate, that didn't work either.


              Am I missing a property here?

              Thanx
              JM

              Comment


                #8
                Disregard... Solved

                JM

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                651 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                370 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                109 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                574 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                577 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X