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

Error on calling 'OnStateChange' method: Object reference...

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

    Error on calling 'OnStateChange' method: Object reference...

    I trying to modify the quantity for different instruments, but every time that I add this
    HTML Code:
                else if (Instrument.MasterInstrument.Name == "NQ" || Instrument.MasterInstrument.Name == "MNQ")
                {
                    Quantity1            = 3;
                    Quantity2            = 1;
                    Quantity3            = 1;
                    Quantity4            = 1;
                }
                else if (Instrument.MasterInstrument.Name == "ES" || Instrument.MasterInstrument.Name == "MES")
                {
                    Quantity1            = 4;
                    Quantity2            = 2;
                    Quantity3            = 1;
                    Quantity4            = 1;
                }​
    I get this error

    Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.
    HTML Code:
            protected override void OnStateChange()
            {
                Print(State);        
    
                if (State == State.SetDefaults)
                {
                ....
                }
                else if (State == State.Configure)
                {
                ....
                }
                else if (State == State.DataLoaded)
                {                
                    Quantity1            = 1;
                    Quantity2            = 1;
                    Quantity3            = 1;
                    Quantity4            = 1;
                }
                else if (Instrument.MasterInstrument.Name == "NQ" || Instrument.MasterInstrument.Name == "MNQ")
                {
                    Quantity1            = 3;
                    Quantity2            = 1;
                    Quantity3            = 1;
                    Quantity4            = 1;
                }
                else if (Instrument.MasterInstrument.Name == "ES" || Instrument.MasterInstrument.Name == "MES")
                {
                    Quantity1            = 4;
                    Quantity2            = 2;
                    Quantity3            = 1;
                    Quantity4            = 1;
                }
            }​

    #2
    Hello xtremel,

    Thanks for your post.

    How are the Quantity variables being declared in your script?

    If you are trying to set the Quantity variables within State.DataLoaded based on a condition, you would need to add your Instrument conditions inside the 'else if (State==State.DataLoaded)' condition.

    The error message "Object reference not set to the instance of an object" means that something in the script is returning null at the time you are accessing it. Debugging steps would need to be taken to determine what exactly is returning null in the indicator.

    Below is a link to a forum post that demonstrates using debugging prints to understand the behavior of a script.
    https://ninjatrader.com/support/foru...121#post791121

    OnStateChange: https://ninjatrader.com/support/help...tatechange.htm

    Let us know if we may assist further.​
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thank you Brandon!!!

      Adding this else if (State==State.DataLoaded) fixed the issue.

      HTML Code:
                  else if ((State==State.DataLoaded) && (Instrument.MasterInstrument.Name == "ES" || Instrument.MasterInstrument.Name == "MES"))

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by AaronKoRn, Today, 09:49 PM
      0 responses
      7 views
      0 likes
      Last Post AaronKoRn  
      Started by carnitron, Today, 08:42 PM
      0 responses
      9 views
      0 likes
      Last Post carnitron  
      Started by strategist007, Today, 07:51 PM
      0 responses
      10 views
      0 likes
      Last Post strategist007  
      Started by StockTrader88, 03-06-2021, 08:58 AM
      44 responses
      3,980 views
      3 likes
      Last Post jhudas88  
      Started by rbeckmann05, Today, 06:48 PM
      0 responses
      9 views
      0 likes
      Last Post rbeckmann05  
      Working...
      X