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 Mindset, 04-21-2026, 06:46 AM
      0 responses
      90 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      137 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      68 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      120 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      72 views
      0 likes
      Last Post PaulMohn  
      Working...
      X