Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NullRef exception when referencing Fibonacci object

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

    NullRef exception when referencing Fibonacci object

    Hello NT8 Support,

    I'm using an indicator which draws a Fib retracement, and I'm creating a strategy that uses this indicator, it seems under this setup, an error is generated when setting the Fibonacci extension to the right.

    (Dummy example)

    Code:
    try
    {
    if (CurrentBar == 15)
    {
    var fib = Draw.FibonacciRetracements(this, "fib", true, 10, Low[10], 1, High[1]);
    fib.IsExtendedLinesRight = true;
    }
    
    
    // Ensure enough bars exist for Value[1]
    // if (CurrentBar == 0)
    // {
    // Value[0] = Input[0];
    // }
    // else
    // {
    // Value[0] = Input[0] * constant1 + constant2 * Value[1];
    // }
    }
    catch (Exception e)
    {
    Print(e.Message);
    Print(e.StackTrace);
    throw;
    }​
    It says:
    Object reference not set to an instance of an object.
    at NinjaTrader.NinjaScript.Indicators.SmaFibonacci.On BarUpdate() in C:\Users\USUARIO\Documents\NinjaTrader 8\bin\Custom\Indicators\SmaFibonacci.cs:line 70
    Indicator 'SmaFibonacci': Error on calling 'OnBarUpdate' method on bar 15: Object reference not set to an instance of an object.​

    Is it because these objects are drawn asynchronously when being referenced inside a strategy?

    Is there a workaround for me to use this Fib retracement object?

    Regards,


    #2
    Hello Waxavi,

    Can you clarify, how is the strategy involved here? Is the strategy using the code shown or is that in the indicator? Does the indicator work when not used in the strategy?

    Comment


      #3
      Hello Jesse,

      The strategy is only referencing the indicator, here-s the strategy code

      Code:
      public class SmaFibStrategy : Strategy
      {
      private SmaFibonacci _smaFib;
      
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "SmaFibStrategy";
      Calculate = Calculate.OnBarClose;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      OrderFillResolution = OrderFillResolution.Standard;
      Slippage = 0;
      StartBehavior = StartBehavior.WaitUntilFlat;
      TimeInForce = TimeInForce.Gtc;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 20;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;
      }
      else if (State == State.Configure)
      {
      }
      else if (State == State.DataLoaded)
      {
      _smaFib = SmaFibonacci();
      }
      }
      
      protected override void OnBarUpdate()
      {
      }
      }​
      And here's the indicator code:

      Code:
      public class SmaFibonacci : Indicator
      {
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Indicator here.";
      Name = "SmaFibonacci";
      IsOverlay = true;
      IsSuspendedWhileInactive = true;
      
      AddPlot(Brushes.Blue, "TestSeries");
      }
      else if (State == State.Configure)
      {
      }
      }
      
      protected override void OnBarUpdate()
      {
      try
      {
      if (CurrentBar == 15)
      {
      var fib = Draw.FibonacciRetracements(this, "fib", true, 10, Low[10], 1, High[1]);
      fib.IsExtendedLinesRight = true;
      }
      }
      catch (Exception e)
      {
      Print(e.Message);
      Print(e.StackTrace);
      throw;
      }
      }
      }​
      Object reference not set to an instance of an object.
      at NinjaTrader.NinjaScript.Indicators.SmaFibonacci.On BarUpdate() in C:\Users\USUARIO\Documents\NinjaTrader 8\bin\Custom\Indicators\SmaFibonacci.cs:line 67
      Indicator 'SmaFibonacci': Error on calling 'OnBarUpdate' method on bar 15: Object reference not set to an instance of an object.

      Comment


        #4
        Hello Waxavi,

        When indicators are used from strategies they will not draw anything on the chart so drawing objects would not be used. You can just add a null check in the indicators code so that property is only set if the object actually exists.

        if(fib != null)
        {

        }

        Comment


          #5
          Hello Jesse,

          Understood, I was planning to retrieve data from this drawn object, so it seems I will have to decouple the drawn object from the data I need .

          Thank you

          Regards,

          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