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

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.NullReferenceExceptionDer 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 keyTValue newValueTValue comparisonValue)
    bei System.Collections.Concurrent.ConcurrentDictionary `2.AddOrUpdate(TKey key, TValue addValue, Func`3 updateValueFactory)
    bei NinjaTrader.NinjaScript.Strategies.SidiSystemBaseDrawOrderLineSL(Order orderChartControl chartControlChartScale chartScaleString orderIdDouble openProfitDouble 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.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by port119, Today, 02:43 PM
    0 responses
    0 views
    0 likes
    Last Post port119
    by port119
     
    Started by Philippe56140, Today, 02:35 PM
    0 responses
    2 views
    0 likes
    Last Post Philippe56140  
    Started by 00nevest, Today, 02:27 PM
    0 responses
    1 view
    0 likes
    Last Post 00nevest  
    Started by Jonafare, 12-06-2012, 03:48 PM
    5 responses
    3,986 views
    0 likes
    Last Post rene69851  
    Started by Fitspressorest, Today, 01:38 PM
    0 responses
    2 views
    0 likes
    Last Post Fitspressorest  
    Working...
    X