Announcement

Collapse
No announcement yet.

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.​
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    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 CarlTrading, 03-31-2026, 09:41 PM
      1 response
      43 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      21 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      30 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      50 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      41 views
      0 likes
      Last Post CarlTrading  
      Working...
      X