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

Object reference error

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

    Object reference error

    Hi,

    I've been running a strategy that has been working and suddenly I'm getting this error. It runs on replay for a day or two and then shuts down after issuing this error in the output window. I'm not even sure how to go about debugging this outside of Visual Studio, which I am unable to do with the version I have.

    I did not make any significant changes to the code and prior to this I was able to run the strategy for an entire 3 months worth of replay data. After undoing my code changes, it still gives me this error:

    Error on calling 'OnBarUpdate' method on bar 490: Object reference not set to an instance of an object.

    Any thoughts on how I can trouble shoot this?

    Thanks!

    #2
    I think the problem has to do with the Order object. I have an oOder object defined so that it can be referenced by other functions. I am assigning corder to the Order object returned by the EnterShortLimit function. My goal is to prevent another order entry from cancelling an existing working order even if the conditions are met. I think this might be causing some issue, but I'm not entirely sure. It was working before and I haven't made any changes to it.

    corder = EnterShortLimit(DefaultQuantity,price, "1st");
    if(corder.FromEntrySignal == "1st" && Position.MarketPosition == MarketPosition.Short){

    Comment


      #3
      Hello,

      Thank you for the post.

      In this case, you would not need visual studio at all, mainly you would just need to use some comments or Prints to find what you are using that is null.

      The error Object reference not set to an instance of an object just means that some object being used is null. We can deduce from the error that it is coming from OnBarUpdate so that gives a good pointer on where to start looking.

      You noted that it may be an order object, you could start by either commenting out this logic or if that affects the script to adversly for the error to occur, you could use some Prints instead:

      Code:
      Print("corder is null?  " + (corder == null));
      if(corder.FromEntrySignal == "1st" && Position.MarketPosition == MarketPosition.Short){
      You could add prints like this through the script on objects you think may be the problem, once you locate the error again you should see a Print similar to:

      corder is null? true
      Error on calling 'OnBarUpdate' method on bar 490: Object reference not set to an instance of an object.
      If the object was null, using any of its sub properties would result in this error. For example the following use:
      Code:
      corder.FromEntrySignal == "1st"
      The correction for a null object error would be to check if it's null before using it:

      Code:
      if(corder != null)
      {
        //object is valid do something
      }

      I look forward to being of further assistance.
      JesseNinjaTrader Customer Service

      Comment


        #4
        Working Strategy and suddenly: Object reference not set to an instance of an object.

        Originally posted by ganbare112 View Post
        Hi,

        I've been running a strategy that has been working and suddenly I'm getting this error. It runs on replay for a day or two and then shuts down after issuing this error in the output window. I'm not even sure how to go about debugging this outside of Visual Studio, which I am unable to do with the version I have.

        I did not make any significant changes to the code and prior to this I was able to run the strategy for an entire 3 months worth of replay data. After undoing my code changes, it still gives me this error:

        Error on calling 'OnBarUpdate' method on bar 490: Object reference not set to an instance of an object.

        Any thoughts on how I can trouble shoot this?

        Thanks!

        Hi ganbare112,

        I hope my situation and solution is similar to yours and you can fix it by applying it.

        Problem:

        I'm using a strategy with two buttons and this same problem started to occur after minor changes to code that to my little programming understanding had nothing to do with it.

        Solution:

        As i had encountered issues before that are introduced by workspaces, i opened my workspace, which uses 4 charts, all using the strategy and only one of them was throwing this error, i deleted the chart and saved the workspace, then i created the chart again, loaded the template i use, loaded my strategy and now my strategy and workspace are working again.

        I did no change to my code and the problem is gone. If it ever occurs i will document it and post it here.

        Best regards,

        RoBott
        Attached Files

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by aa731, Today, 02:54 AM
        0 responses
        4 views
        0 likes
        Last Post aa731
        by aa731
         
        Started by thanajo, 05-04-2021, 02:11 AM
        3 responses
        470 views
        0 likes
        Last Post tradingnasdaqprueba  
        Started by Christopher_R, Today, 12:29 AM
        0 responses
        10 views
        0 likes
        Last Post Christopher_R  
        Started by sidlercom80, 10-28-2023, 08:49 AM
        166 responses
        2,237 views
        0 likes
        Last Post sidlercom80  
        Started by thread, Yesterday, 11:58 PM
        0 responses
        4 views
        0 likes
        Last Post thread
        by thread
         
        Working...
        X