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 NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      54 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
      72 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      44 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      49 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X