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 Mindset, 04-21-2026, 06:46 AM
    0 responses
    88 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    134 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    119 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    67 views
    0 likes
    Last Post PaulMohn  
    Working...
    X