Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error on running backtest: Object reference not set to an instance of an object

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

    Error on running backtest: Object reference not set to an instance of an object

    Has anyone ever gotten the following error?

    "Error on running backtest: Object reference not set to an instance of an object."

    One of my strategies that I've ran a million times with a ton of different instruments across decades of data and thousands of trades, without previous error, is giving the above error for one instrument, and one instrument only. I really think it's a data problem. I can even narrow it down to what date range sets off the error. Any ideas?

    Thanks!

    #2
    Hi Style,

    That message comes up whenever NinjaTrader attempts to access an empty object in the script. To further narrow down the cause of the error you can use Try - Catch blocks:


    If you're only seeing it for one instrument and otherwise it looks OK, it could hint to a data issue, but could also be due to unusual code flow caused by the particulars of that instrument. Using Try - Catch will let you know more information about the object causing the exception and hopefully provide a clue where to go next.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Do you have OnPositionUpdate() in your code?
      That sometimes creates this error in backtests. (While working fine in realtime use.)

      Comment


        #4
        Where would try-catch write the more descriptive error message to? I tried putting all the code inside the OnBarUpdate section inside a try statement, but it didn't catch anything, as far as I could tell. There was nothing in the log file except the original error message.

        I'm not using OnPositionUpdate.

        Thanks!

        Comment


          #5
          Below is the catch block from the sample. You can work within that block to debug your code. From there you can print CurrentBar, BarsInProgress and anything else useful in tracking it. You can place it around entire OnBarUpdate(), but can also use multiple try-catch blocks within your code if you need even more control.


          // This is the block where we handle the caught exception.
          catch (Exception e)
          {
          /* With our caught exception we are able to generate log entries that go to the Control Center logs and also print more detailed information
          about the error to the Output Window. */

          // Submits an entry into the Control Center logs to inform the user of an error
          Log("SampleTryCatch Error: Please check your indicator for errors.", LogLevel.Error);

          // Prints the caught exception in the Output Window
          Print(Time[0] + " " + e.ToString());
          }
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            Ryan, I used the sample catch statement, didn't change a single character. In the output window it just said "Error on running backtest: Object reference not set to an instance of an object." I didn't get anything different than on previous tests, nothing in the format "Time[0] + " " + e.ToString()". Does this mean that the error is occurring inside an indicator called by the strategy and not in the strategy itself?

            Thanks!

            Comment


              #7
              The log tab of the control center should provide info on whether it's an indicator or strategy and the exact script causing the exception.

              You may need to simplify a good bit and change the try catch blocks to refer to a narrower section of code, and of course expand from OnBarUpdate() if you're using other event handlers.
              Ryan M.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              648 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              369 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              108 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              572 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              573 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X