Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Object reference not set to an instance of an object Error

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

    Object reference not set to an instance of an object Error

    Hi,

    I am clearly doing something stupid and even though it is a simple thing, for the world I cannot see where the error is. Any insight would be greatly appreciated. Thank you.

    In my strategy I define a variable for my custom indicator:
    Code:
    private NinjaTrader.NinjaScript.Indicators.MyCustomIndicator myCustomIndicator;
    In State == State.DataLoaded I have
    Code:
    myCustomIndicator = NinjaTrader.NinjaScript.Indicators.MyCustomIndicator();
    This fails to compile with the error:
    NinjaTrader.NinjaScript.Indicators.MyCustomIndicat or' is a 'type', which is not valid in the given context.
    Weird.

    So I change this line to
    Code:
    myCustomIndicator = MyCustomIndicator();
    And add
    Code:
    AddChartIndicator(myCustomIndicator);
    The script compiles fine but when the strategy is run, the line
    Code:
    myCustomIndicator = MyCustomIndicator();
    Generates the error:
    Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.
    I'm sure the solution is simple, I just can't see it.

    #2
    Hello.

    Thanks for the post.

    There is something going wrong when instantiating the custom indicator, If your strategy is doing the following then the initialization of the custom indicator is failing and you would have to debug the indicator's OnStateChange method.

    Code:
    public class MyCustomStrategy : Strategy
    	{
    		private NinjaTrader.NinjaScript.Indicators.MyCustomIndicator myCustomIndicator;
                    ...
    Code:
    else if (State == State.DataLoaded)
    			{
    				myCustomIndicator = MyCustomIndicator();
    				AddChartIndicator(myCustomIndicator);
    			}
    Are you able to load the custom indicator by itself on a chart?

    Could you post the OnStateChange method of the custom indicator?

    I look forward to your reply.

    Comment


      #3
      Hi Chris,

      Thank you for your reply. I do believe that there is an issue with instantiating the custom indicator. I will check the indicator OnState method as suggested.

      As an aside, the indicator compiles and runs fine by itself. It does, however, have expandable property classes that are not decorated with the [NinjaScriptProperty] attribute and I wonder whether this is causing a constructor issue.

      EDIT: So I have disected my indicator in detail and I think I have found the problem:
      The indicator uses ChartBars and ChartControl for various things. This is not an issue when the indicator runs on the chart. It is an issue when called from a strategy as ChartBars and ChartControl are null. How do I fix this?
      Last edited by Zeos6; 03-28-2018, 11:22 AM.

      Comment


        #4
        Hello.

        Thanks for the reply.

        I am able to use ChartBars and ChartControl in my testing. Are you trying to load this strategy from a chart or from the Strategies tab of the Control Center?

        I look forward to your reply.

        Comment


          #5
          Hi Chris,

          Figured it out - finally! Was not checking ChartBars and ChartControl for null in the indicator. Worked as an indicator but created issues in Strategy testing. Finally figured it out from NinjaScript Best Practices and the section on accessing elements on the UI. Thank you.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          52 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          130 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          70 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          43 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          47 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X