Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NullReferenceException in OnRender

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

    NullReferenceException in OnRender

    Hi, In OnRender I run this code:

    Code:
    -------here in OnRender()--------------
    if (Position.MarketPosition != MarketPosition.Flat && !isPositionClosedXSendToMarket && chartTraderSL.Count > 0)
    TriggerCustomEvent(o =>
    {
       foreach (var item in chartTraderSL.ToArray())
    [COLOR=#e74c3c]DrawOrderLineSL[/COLOR](item.Value.Order, chartControl, chartScale, item.Key, item.Value.OpenLoss, item.Value.TickValue);
    }, null);
    
    
    ---------here in [COLOR=#e74c3c]DrawOrderLineSL[/COLOR]---------
    var newValueWidth = new StopLoss { OrderLabelLayoutWidth = rectTextOrderLabelSL.Width + rectQtyOrderLabelSL.Width};
    
    chartTraderSL.AddOrUpdate(orderId, newValueWidth, (key, oldValueWidth) =>
    {
       if (newValueWidth.OrderLabelLayoutWidth != oldValueWidth.OrderLabelLayoutWidth)
            oldValueWidth.OrderLabelLayoutWidth = newValueWidth.OrderLabelLayoutWidth;
    
       return oldValueWidth;
    });
    sometimes i get a NullReferenceException. can anyone tell me what might be throwing this in the code and how best to fix it?

    PHP Code:
    04.03.2021 09:27:34:000 > [SidiSystemBase][DrawOrderLineSL]-[ERROR]-System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
    bei NinjaTrader.NinjaScript.Strategies.StopLoss.Equals (StopLoss other)
    bei System.Collections.Generic.GenericEqualityComparer `1.Equals(T x, T y)
    bei System.Collections.Concurrent.ConcurrentDictionary `2.TryUpdate(TKey key, TValue newValue, TValue comparisonValue)
    bei System.Collections.Concurrent.ConcurrentDictionary `2.AddOrUpdate(TKey key, TValue addValue, Func`3 updateValueFactory)
    bei NinjaTrader.NinjaScript.Strategies.SidiSystemBase. DrawOrderLineSL(Order order, ChartControl chartControl, ChartScale chartScale, String orderId, Double openProfit, Double tickValue) 
    
    sidlercom80
    NinjaTrader Ecosystem Vendor - Sidi Trading

    #2
    Hello sidlercom80,

    Thank you for the post.

    Based on the error mentioning a ConcurrentDictionary and you are using what appears to be a dictionary I would guess it may be the chartTraderSL variable. You could add a condition like:

    Code:
    if(chartTraderSL != null)
    {
        chartTraderSL.AddOrUpdate(orderId, newValueWidth, (key, oldValueWidth) => {
          if (newValueWidth.OrderLabelLayoutWidth != oldValueWidth.OrderLabelLayoutWidth) oldValueWidth.OrderLabelLayoutWidth = newValueWidth.OrderLabelLayoutWidth;
            return oldValueWidth;
        });
    }
    I look forward to being of further assistance.
    Last edited by NinjaTrader_Jesse; 03-04-2021, 08:34 AM.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    80 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    40 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    64 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    63 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    54 views
    0 likes
    Last Post CarlTrading  
    Working...
    X