• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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.
    Chris L.NinjaTrader Customer Service

    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.
        Chris L.NinjaTrader Customer Service

        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 maggie3, Today, 02:05 AM
          0 responses
          3 views
          0 likes
          Last Post maggie3
          by maggie3
           
          Started by qewcool, Today, 01:14 AM
          0 responses
          3 views
          0 likes
          Last Post qewcool
          by qewcool
           
          Started by fredrikn, 05-08-2019, 04:01 AM
          17 responses
          669 views
          1 like
          Last Post emtrader  
          Started by zacharydw00, Yesterday, 10:19 PM
          0 responses
          3 views
          0 likes
          Last Post zacharydw00  
          Started by chafan11, Yesterday, 09:28 PM
          0 responses
          7 views
          0 likes
          Last Post chafan11  
          Working...
          X