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 NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    62 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    134 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    75 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    45 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    50 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X