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

OnRenderTargetChanged error when loading template

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

    OnRenderTargetChanged error when loading template

    I have modified a copy of the ZigZag indicator to draw text and lines at inflection points. It works without error when opening a workspace that contains a relevant chart. If I then load a template to the chart that contains the indicator, OnRenderTargetChanged throws this error:
    2017-03-30 05:53:51:604|3|16|03/30/17 05:53:51 TROUBLE! gZigZag.OnRenderTargetChanged
    Object reference not set to an instance of an object.
    Source: NinjaTrader.Custom
    StackTrace: at NinjaTrader.NinjaScript.Indicators.gws.gZigZag.OnR enderTargetChanged()
    TargetSite: Void OnRenderTargetChanged()
    ChartBars:
    I have other indicators that also use OnRenderTargetChanged() to manage SharpDX objects and those indicators exhibit the same problem of throwing an error only when loading a template.

    The attached zip file contains the indicator, template, and workspace. To reproduce the error, first connect and then open the Sandbox OnRenderTargetChanged workspace, which contains a 1-Minute chart with the gZigZag indicator. No error should occur.

    Next, load the aZigZagOnRenderTargetChanged template, which should throw the Object reference not set error.

    Another curiosity is that the string following "ChartBars:" seems to be empty. It gets assigned in the State.DataLoaded block and normally contains information about the chart's bars object.
    Attached Files

    #2
    Hello,

    Thank you for posting.

    I reviewed the attached file and do see the error while completing the steps you had.
    Object reference not set to an instance of an object. specifically, this indicates that some object you are using is null while you try to use it. This can occur when the render target changes or other actions on the chart complete. Depending on what objects you are using, you would likely need to add null checks how you currently are. I noted that you are not checking for null on all objects that you use that can be null such as ChartControl and some of the brushes being used. I added the following and can see the OnRenderTarget error disappear but then we get other errors from OnRender.

    Code:
    if(cdxbrush_high!= null && ColorHigh != null)cdxbrush_high	= ColorHigh.ToDxBrush(RenderTarget);
    if(cdxbrush_high!= null && ColorLow != null)cdxbrush_low	= ColorLow.ToDxBrush(RenderTarget);
    if(cdxtextformat!= null && ChartControl != null)cdxtextformat 	= ChartControl.Properties.LabelFont.ToDirectWriteTextFormat()
    ;

    Due to the amount of logic in this sample, this would be outside what our support could provide debugging assistance with. I would instead suggest reducing this further to locate other error locations in the logic and to ensure to check for null objects wherever possible.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      I noted that you are not checking for null on all objects that you use that can be null such as ChartControl and some of the brushes being used. I added the following and can see the OnRenderTarget error disappear but then we get other errors from OnRender.

      Code:
      if(cdxbrush_high!= null && ColorHigh != null)cdxbrush_high    = ColorHigh.ToDxBrush(RenderTarget);
      if(cdxbrush_high!= null && ColorLow != null)cdxbrush_low    = ColorLow.ToDxBrush(RenderTarget);
      if(cdxtextformat!= null && ChartControl != null)cdxtextformat     = ChartControl.Properties.LabelFont.ToDirectWriteTextFormat();
      The culprit was ChartControl, which apparently is null when loading a template. Your code errs in that the SharpDX brushes would never get assigned, since you're only assigning them when they are not null. So, of course when OnRender() attempts to use them it will throw an error.

      The following code fixes the problem by assigning fallback objects in case the desired object is null:
      Code:
      cdxbrush_high = ColorHigh != null ? ColorHigh.ToDxBrush(RenderTarget) : Brushes.DarkRed.ToDxBrush(RenderTarget);
      cdxbrush_low = ColorLow != null ? ColorLow.ToDxBrush(RenderTarget) : Brushes.Green.ToDxBrush(RenderTarget);
      cdxtextformat = ChartControl != null ? ChartControl.Properties.LabelFont.ToDirectWriteTextFormat()
                              : new Gui.Tools.SimpleFont("Consolas", 14).ToDirectWriteTextFormat(); [COLOR=SeaGreen]// just in case ChartControl is null[/COLOR]
      Thanks for pointing me in the right direction. In the future, I shall be diligent in always checking for null.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by sofortune, 05-18-2024, 11:48 AM
      2 responses
      32 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Started by Zach55, 02-19-2024, 07:22 PM
      2 responses
      65 views
      0 likes
      Last Post lbadisa1  
      Started by JGriff5646, Yesterday, 05:47 PM
      1 response
      14 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Started by AlphaOptions, 06-18-2013, 08:24 AM
      9 responses
      2,203 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by ttrader23, Yesterday, 09:33 AM
      3 responses
      29 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Working...
      X