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

OnMarketDepth: Object reference not set to an instance of an object.

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

    OnMarketDepth: Object reference not set to an instance of an object.

    Why should this code be throwing intermittent errors?
    Code:
    protected override void OnMarketDepth(MarketDepthEventArgs marketDepthUpdate)
    {
    ...if (cdict_barvolume.TryGetValue(Instrument.MasterInstrument.RoundToTickSize(marketDepthUpdate.Price), 
                                out vii_value))
    [B]             { // *** here's the trouble: had not enclosed the following block within { }[/B]
                        vii_value = cdict_barvolume[Instrument.MasterInstrument.RoundToTickSize(marketDepthUpdate.Price)];
                        if (marketDepthUpdate.MarketDataType == MarketDataType.Ask)
                            vii_value.volatask = 0;
                        else if (marketDepthUpdate.MarketDataType == MarketDataType.Bid)
                            vii_value.volatbid = 0;
             [B]     } // *** [/B]
    ...         
    }
    Last edited by tradesmart; 12-15-2016, 07:21 AM. Reason: Corrected problem code

    #2
    Hello,

    Thank you for the post.

    Without debugging the script it would be very difficult to say exactly what the problem is. I can tell you what the error means which may help you isolate the problem while you debug the script.

    The error: Object reference not set to an instance of an object relates to using an object that is null.

    For example, you are currently checking:
    Code:
     if (marketDepthUpdate == null)
    If we happened to use a property of marketDepthUpdate before this point, we would get this error. Likely the same problem exists nested further in your logic potentially with a different object.

    The most simple way to find the problem would be to comment out the code until the error no longer happens. Once you reach this point, start uncommenting code line by line until you reach the exception again.

    This would help pinpoint what line is throwing the exception and allow you to handle it accordingly.

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

    Comment


      #3
      Well, here's the thing: the indicator whose OnMarketDepth code this is runs fine without a hitch for quite some time. Then it throws the error, which I trap in the try/catch so that it continues on its merry way for a half hour or so, until it throws the error again. Rinse and repeat. The error causes no interruption of the indicator's performance; only the log window reveals something amiss. But I don't like to see errors, even seemingly innocuous ones like this.

      The only objects in play that I can see are the two dictionaries, both of which are instantiated in their declarations at the class level. Am I missing something?

      It seems, although I cannot be certain, that increased market activity might have something to do with this.

      Comment


        #4
        Hello,

        For this because you are handling the exception and only later reviewing it, I would suggest to remove the try/catch and allow the exception to stop the script. This should produce an error in the output window/ log tab for review of the Time it occurred.

        You can also place prints for the objects which could potentially be null right before they are used in the script. This could include NinjaScript objects or custom objects like the dictionaries. Once the script stops, the prints should be the last thing seen to address what is null at that point without continuing for awhile past the error. If you are catching the exception and all that is happening is the note in the log, you likely have handled it but with any exception there is a reason that can be found by further debugging the logic.

        Just as an example, you could use something like the following print:

        Print("marketDepthUpdate null: " + (marketDepthUpdate == null) + " marketDepthUpdate.Instrument null: " + (marketDepthUpdate.Instrument == null));

        etc.. More details should highlight what is null.

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

        Comment


          #5
          I think I've got it. I wrapped each code block of OnMarketDepth in its own try/catch to drill down into the code. This seems to have narrowed the problem down to the Operation.Remove section, where I had neglected to enclose the if (true) block in braces. This resulted in attempts to update a dictionary row without having first retrieved it.

          Mea culpa. This is not the first time that missing braces have tripped me up. As a matter of style, I should just go with the convention I thought I'd assimilated 20 years ago and use braces always, even around a single statement.

          Thanks so much for your help, Jesse.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by kevinenergy, 02-17-2023, 12:42 PM
          118 responses
          2,778 views
          1 like
          Last Post kevinenergy  
          Started by briansaul, Today, 05:31 AM
          0 responses
          7 views
          0 likes
          Last Post briansaul  
          Started by fwendolynlpxz, Today, 05:19 AM
          0 responses
          4 views
          0 likes
          Last Post fwendolynlpxz  
          Started by traderqz, Yesterday, 12:06 AM
          11 responses
          28 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by PaulMohn, Today, 03:49 AM
          0 responses
          8 views
          0 likes
          Last Post PaulMohn  
          Working...
          X