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

Good old "Object reference not set to an instance of an object"

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

    Good old "Object reference not set to an instance of an object"


    NT Folks, I'd like to get your insight into the workspace errors that can cause and correct, "Object reference not set to an instance of an object."

    It's not uncommon I get this error when activating this strategy, and the strategy fails to activate.

    In the output window, I get error:

    Strategy 'Base1': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.

    Well, I've reduced my OnStateChange to debug statements only:

    protected override void OnStateChange() {
    if (State == State.SetDefaults) {
    Print("At time " + Time[0] + " Statement 5");
    } else if (State == State.DataLoaded) {
    Print("At time " + Time[0] + " Statement 6");
    } else if (State == State.Configure) {
    Print("At time " + Time[0] + " Statement 7"); }
    }

    No object is being referenced within OnStateChange. I've found I'm able to <sometimes> correct this error by:

    - Waiting. Sometimes if I wait 15 minutes, to the next day, if I then activate the exact same strategy and parameters, then the strategy activates.
    - Resetting NT. If I close down NT and restart it, then the strategy may activate.
    - Making a copy of the strategy. If I copy the strategy to a new name, and then load up into the same chart and parameters, then the strategy may activate.
    - Within the chart, if I reload all Ninjascript, or reload all historical data, then this issue may be corrected.

    From a prior support discussion on this issue:

    I've started getting this error the last few days whenever I load my main Workspace: Unhandled Exception: Object reference not set to an instance of an object. - It seems to be accompanied by NinjaTrader not saving my strategies applied to charts - so when I restart the program, the &quot;Strategies&quot; tab on the main/home


    the advice is to restart the workspaces, and maybe repair the database.

    It really seems like this error is caused more by the state of NT, than a coding issue. I really dislike the Restart NT solutions. As I'm running and tracking hundreds of strategies during the day, resetting NT is a major issue. Resetting zeros out strategy realized/unrealized amounts, making it difficult to track strategy performance during the day and over the course of the week.

    Therefore, it seems like this boils down to two issues:

    - Do you have any direction for a code solution?
    - Since this error seems to be NT state driven (internal), can you log an enhancement request to dig into this issue and resolve?

    Thanks for your help,













    #2
    Hello timmbbo,

    Thank you for your post.

    The prints in your snippet reference Time[0] which is not available until OnBarUpdate()
    Code:
    protected override void OnStateChange() {
    if (State == State.SetDefaults) {
    Print("At time " + Time[0] + " Statement 5");
    } else if (State == State.DataLoaded) {
    Print("At time " + Time[0] + " Statement 6");
    } else if (State == State.Configure) {
    Print("At time " + Time[0] + " Statement 7"); }
    }​
    We have a tip page in the help guide about null references in particular here:


    I suggest adding a numbered print statement every few lines of code. You could even add a print in OnStateChange() that prints the state just to see what state is hit prior to the error being thrown:

    Code:
    protected override void OnStateChange()
    {
    Print("State: " + State);
    }​
    As for the numbered prints, such as throughout OnBarUpdate(), every two or three lines you should print something like the following:
    Code:
    protected override void OnBarUpdate()
    {
    Print("Debug 1");
    // few lines of code here
    Print("Debug 2");
    // another few lines of code
    Print("Debug 3");
    // etc
    }
    From there, keep the NinjaScript Output window open while running your script. Monitor what was the last state mentioned in the output window and which debug print number shows up in the output before the error is thrown. This can help you to narrow down which line of your script is causing the error, then you can add a check for a null reference to prevent it from being thrown in the future.

    For more information on using debugging prints, OnStateChange() and what information is available in each state, and additional debugging tips, please see the links below:Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3

      Emily, thanks, although that's not it. I'm pretty sure the error is within the chart, and not the code itself.

      First, I've cut the code down to nothing.

      namespace NinjaTrader.NinjaScript.Strategies {
      public class Base1 : Strategy {
      private Order entryOrder = null;
      protected override void OnStateChange() {
      if (State == State.SetDefaults) {
      Description = NinjaTrader.Custom.Resource.NinjaScriptStrategyDes criptionSampleMACrossOver;
      Name = "Base1";
      SMAer = 100;
      ProfitTicks = 4;
      CT = 0;
      } else if (State == State.DataLoaded) {
      } else if (State == State.Configure) {
      }
      }

      protected override void OnBarUpdate() {
      if (CurrentBar < SMAer) return;
      }

      protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError) {
      }

      region Properties
      [Range(1, int.MaxValue), NinjaScriptProperty]
      [Display(ResourceType = typeof(Custom.Resource), Name = "SMA", GroupName = "NinjaScriptStrategyParameters", Order = 0)]
      public int SMAer
      { get; set; }

      [Range(1, int.MaxValue), NinjaScriptProperty]
      [Display(ResourceType = typeof(Custom.Resource), Name = "ProfitTicks", GroupName = "NinjaScriptStrategyParameters", Order = 1)]
      public int ProfitTicks
      { get; set; }

      [Range(0, 1), NinjaScriptProperty]
      [Display(ResourceType = typeof(Custom.Resource), Name = "Counter Trade", GroupName = "NinjaScriptStrategyParameters", Order = 2)]
      public int CT
      { get; set; }

      #endregion
      }
      }​

      Now, I ask you to consider:

      Screen shot foo1.jpg: When I activate this nothing-strategy, in a chart with other data series and strategies, I get a hard error.
      Screen shot foo2.jpg: You can see the next error for activation
      Screen shot foo3.jpg: When I create a brand-new chart containing only the target data series, and then activate the same strategy within this chart, the strategy then successfully activates.
      Screen shot foo4.jpg: Putting the non-working chart side-by-side, to the working chart. As you can see, where the strategy fails, that chart has more complexity.

      As you can see, the same strategy fails within one chart, but works in another when applied to same data series.

      Your thoughts? Thanks,


      Attached Files

      Comment


        #4

        Emily, hello again - I stumbled onto the solution.

        When I was activating the strategy, I had it set to a sim account with a negative balance. When I created a new sim account with a positive balance, and then set the strategy to that account, then the strategy successfully activates. I can only imagine NT never contemplated or tested this particular scenario, so it's not totally surprising there is unexpected or erroneous behavior in this case.

        Perhaps you can log this as an enhancement request? Have a negative account balance should not cause the code to bomb out.

        Thanks,




        Attached Files

        Comment


          #5
          Originally posted by timmbbo View Post
          Emily, hello again - I stumbled onto the solution.

          When I was activating the strategy, I had it set to a sim account with a negative balance. When I created a new sim account with a positive balance, and then set the strategy to that account, then the strategy successfully activates. I can only imagine NT never contemplated or tested this particular scenario, so it's not totally surprising there is unexpected or erroneous behavior in this case.

          Perhaps you can log this as an enhancement request? Have a negative account balance should not cause the code to bomb out.

          Thanks,



          Hello timmbbo,

          Thank you for the update.

          I am curious; are you able to get the same errors when attempting to enable the Sample MA Crossover on a sim account with a negative balance?

          I look forward to hearing the results.
          Emily C.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by fx.practic, 10-15-2013, 12:53 AM
          5 responses
          5,403 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by Shai Samuel, 07-02-2022, 02:46 PM
          4 responses
          94 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by DJ888, Yesterday, 10:57 PM
          0 responses
          6 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by MacDad, 02-25-2024, 11:48 PM
          7 responses
          158 views
          0 likes
          Last Post loganjarosz123  
          Started by Belfortbucks, Yesterday, 09:29 PM
          0 responses
          8 views
          0 likes
          Last Post Belfortbucks  
          Working...
          X