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

Help with "Object reference not set" error in OnBarUpdate()

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

    Help with "Object reference not set" error in OnBarUpdate()

    Hello!

    I'm new to NinjaTrader*(as a convert from FXCM) and am converting my indicators from Lua to C#. I'm getting a handle on things for the most part, but apparently still have confusion with the way bar timing works.

    I have a "trend state" indicator being consumed by another indicator and receive the following error:
    Indicator 'TrendState': Error on calling 'OnBarUpdate' method on bar 50: Object reference not set to an instance of an object.

    The TrendState indicator works just fine on its own. I know I'm close but have something not quite right. The code is below. I tried to follow how the @ATR, but can't figure this out for the life of me.

    What am I missing or doing wrong?

    Code:
    
    		//
    		// PARENT INDICATOR CODE
    		//
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = @"My Description";
                    Name = "TrendState";
                    Calculate = Calculate.OnEachTick;
                    IsOverlay = true;
    
                    DisplayInDataBox = true;
                    PaintPriceMarkers = false;
                    DrawHorizontalGridLines = false;
                    DrawVerticalGridLines = false;
    
                    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    IsSuspendedWhileInactive = true;
    
                    AddPlot(ChartControl.Properties.ChartBackground, "TrendState");
                }
            }
            protected override void OnBarUpdate()
            {
                if (CurrentBar < 50)
                {
    				// Need at least 50 bars to work properly, so return "undetermined" state
                    Value[0] = 0;
                    return;
    			}
    			else
    			{
    				// Get the state of the trend
    				Value[0] = DoSomeLogicThatDeterminesState();
    				
    				// Do some visual UI stuff here to represent state
    			}
    		}		
    
    		
    		//
    		// CONSUMING INDICATOR CODE
    		//
    		protected override void OnBarUpdate()
    		{
                Print("Candle closed; Opened at: " + Time[0].ToString() + "; CurrentBar: " + CurrentBar + "; O: " + Open[0] + "; C: " + Close[0] + "; H: " + High[0] + "; L: " + Low[0] + "; State: " + State.ToString());
    
                double state = TrendState()[0];
                Print(String.Format("Current State: {0}", state));
            }

    #2
    Hello MarkWise,

    Thank you for your note.

    If you add a current bar check to the hosting indicator, do you still experience the issue?

    For example,

    Code:
    protected override void OnBarUpdate()
    {
    if (CurrentBar < 50) return;
    
    Print("Candle closed; Opened at: " + Time[0].ToString() + "; CurrentBar: " + CurrentBar + "; O: " + Open[0] + "; C: " + Close[0] + "; H: " + High[0] + "; L: " + Low[0] + "; State: " + State.ToString());
    
    double state = TrendState()[0];
    Print(String.Format("Current State: {0}", state));
    }
    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      I do actually. If you look at the OnBarUpdate() method in my sample code, I'm doing the following:

      Code:
           if (CurrentBar < 50)
                  {
      	       // Need at least 50 bars to work properly, so return "undetermined" state
                      Value[0] = 0;
                      return;
      	}
      	else
      	{
      		// Get the state of the trend
      		Value[0] = DoSomeLogicThatDeterminesState();
      				
      		// Do some visual UI stuff here to represent state
      	}

      Comment


        #4
        What happens when CurrentBar = -1?

        It's less than 50, so you're trying to assign a value to a bar that does not exist yet, i.e., at index 0.

        Therefore, Value[0] cannot be set to anything. You might try commenting out that line or at least check to see that CurrentBar >=0 before assigning a value.

        Comment


          #5
          Hello,

          Thank you tradesmart.

          MarkWise, Please replace what you have in your current bar check with,

          Code:
          if (CurrentBar < 50) return;
          In both your hosted and hosting indicator and let us know if that fails to resolve the issue.

          Please let us know if you need further assistance.
          Alan P.NinjaTrader Customer Service

          Comment


            #6
            Solved!

            So after much debugging I realized what the issue is...

            The indicator I was calling also does some UI manipulation. In that logic, I was referencing the "ChartControl" object which apparently doesn't exist unless the indicator is actually sited on the chart itself.

            By simply wrapping the UI logic in a "if (ChartControl != null) ..." block, I was able to solve the issue.

            Thanks everyone for your responses!!!!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by adeelshahzad, Today, 03:54 AM
            5 responses
            30 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by stafe, 04-15-2024, 08:34 PM
            7 responses
            31 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by merzo, 06-25-2023, 02:19 AM
            10 responses
            823 views
            1 like
            Last Post NinjaTrader_ChristopherJ  
            Started by frankthearm, Today, 09:08 AM
            5 responses
            18 views
            0 likes
            Last Post NinjaTrader_Clayton  
            Started by jeronymite, 04-12-2024, 04:26 PM
            3 responses
            43 views
            0 likes
            Last Post jeronymite  
            Working...
            X