Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Null Exception Errors in New Version of NT

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

    Null Exception Errors in New Version of NT

    Hi,

    I just installed the new release of NT, version 8.0.14.1 64-bit. I have an indicator that reuses DX brushes and strokes for OnRender(). This is handled in OnRenderTargetChanged(). The script ran properly (no issues) with the previous version of NT. The issue seems to lie in lines of the type...

    Code:
    defaultStroke.RenderTarget = RenderTarget;
    Am I missing something? Has anything changed in this new version of NT?

    The error I get is..
    Error on calling 'OnRenderTargetChanged' method: System.NullReferenceException: Object reference not set to an instance of an object.
    The complaint seems to be that defaultStroke is null.

    Any insight and help would be greatly appreciated especially as this was not an issue previously, and just arose with this new version of NT. Thank you.

    #2
    Hello Zeos6,

    Thanks for the post.

    Could you please make a simplified version of the script that still reproduces the issue then post it here? You may also send it into platformsupport[at]ninjatrader[dot]com and add the link to this forum post in the body of the email.

    I will look forward to your reply.

    Comment


      #3
      Hi ChrisL,

      Just use
      Code:
      private Brush defaultBrush = Brushes.Transparent;
      NinjaTrader.Gui.Stroke defaultStroke;
      In State == State.Configure set
      Code:
      // Define brush
      Brush desiredDefaultSolidBrush = defaultBrush as SolidColorBrush;
      defaultBrush = desiredDefaultSolidBrush.Clone();
      defaultBrush.Opacity = 0.0f;
      defaultBrush.Freeze();    
                      
      // Define strokes
      defaultStroke = new Stroke(defaultBrush, DashStyleHelper.Dot, 0f);
      And
      Code:
              public override void OnRenderTargetChanged()
              {            
                  // Explicitly dispose of DX brushes...
                  if (defaultBrushDX != null)
                      defaultBrushDX.Dispose();
      
                  if (RenderTarget != null)
                  {
                      // Explicitly set DX brushes and brush RenderTarget
                      try
                      {
                          // Painting brush
                          defaultBrushDX = defaultBrush.ToDxBrush(RenderTarget);
      
                      }
                      catch (Exception exception)
                      {
                          Log(exception.ToString(), LogLevel.Error);
                      }
                      
                       // Explicitly set the Stroke RenderTarget
                      defaultStroke.RenderTarget = RenderTarget;
                  }
              }
      Then just paint something in OnRender().

      Comment


        #4
        Hello Zeos6,

        Thanks for your patience.

        OnRenderTarget is called before State.Configure so that is why we were getting a null reference error. You can fix that by putting your State.Configure logic into State.SetDefaults.

        There is a new indicator in the platform on update 8.0.14.1 called Price Line that uses a Stroke public property and then used in OnRender to style a line. That indicator would be a good reference if you need to create a brush with custom properties rather than having to set the Stroke's RenderTarget.

        Please let me know if that implementation will not work for your needs.

        Comment


          #5
          Hi ChrisL,

          Do you know why this was changed from the previous version of NT? I can see this as a work around but logically it doesn't make sense. Also, does that mean that brushes have to be initialed in State.SetDefaults as well? Thank you for the reference. Will have a look. I really would much prefer to have things work as they did previously.

          EDIT: The approach of the PriceLine indicator will not work for me. I do not allow the user to select the stroke. I create it and use it internally based on other choices that were made.

          EDIT2: By the way. the DX extensions also fail to work because of this so this now becomes an issue with brushes as well. Wow! Why was this behaviour changed?

          EDIT3: Given this scenario, how do you now reuse DX brushes and strokes?
          Last edited by Zeos6; 06-01-2018, 11:49 PM.

          Comment


            #6
            Hello Zeos6,

            Thanks for the reply.

            There is no documentation for the reason OnRenderTargetChanged is called before State.Configure. Generally, we recommend the following things:

            *If possible, just use and dispose of brushes inside of OnRender
            *If you're saving them for performance reasons, use OnRenderTargetChanged.
            *If possible, do all initialization work that is not state dependent in State.SetDefaults.

            Setting up the stroke in State.SetDefaults, as described in the help guide, will not effect changing the brush at a later time in your script.

            https://ninjatrader.com/support/help...roke_class.htm - Stroke Class

            Please let us know if you have any questions.

            Comment


              #7
              Thank you for your reply ChrisL, but you response is way off the mark and shows a complete lack of understanding of what is going on here. Lets recap...

              1. In the previous version of NT, R13, State == State.Configure executes BEFORE OnRenderTargetChanged(). This allowed for the efficient reuse of DX brushes, strokes, and resources.Please check the code samples NT has provided in the past regarding the reuse of DX brushes. Please also check NT8 documentation on custom rendering using ShaprDX.

              In the current version of NT, R14, State == State.Configure executes AFTER OnRenderTargetChanged(). This is a COMPLETE deviation from the previous mode of NT operation. This completely negates the efficient use of DX resources. For you to say
              *If possible, just use and dispose of brushes inside of OnRender
              shows a complete lack of understanding of using DX resources efficiently. Frankly it also sounds like you re just blowing off the question because you cannot/or do not want to deal with it. In fact in 2015/6 it was brought to your attention that there is a potential issue with when RenderTarget becomes available. Clearly this flaw in NT has not been addressed to this date.

              2. You state
              *If you're saving them for performance reasons, use OnRenderTargetChanged.
              I wish you would understand the issue. Lets try this again. Since OnRenderTargetChanged() is now - for some unexplicable reason - called before State.Configure, RenderTarget is now not available to use DX resources efficiently. It was available in the previous version of NT.

              3. You state
              *If possible, do all initialization work that is not state dependent in State.SetDefaults.
              This is only sensible if you are allowing user input for brushes and strokes. It doesn't make sense for internally generated DX resources. This really is not a sensible suggestion in the general case.

              The issue really is that the current version of NT breaks using DX resources. The error is at your end and needs to be corrected.

              Comment


                #8
                Hello Zeos6,

                Thank you for your patience.

                I am able to correct the error from the code snippets you supplied by simply moving the code that is in State.Configure to State.SetDefaults. I also do not see in the documentation nor in the previous version where this would work without having the code in State.SetDefaults.

                Take our OnRenderTargetChanged() example at the Help Guide page linked below. Notice all setting of the Brush is performed first in State.SetDefaults. While, yes this is a bit different from your case it still shows the use of State.SetDefaults.

                Please let me know if you have any questions.
                Last edited by NinjaTrader_PatrickH; 06-06-2018, 09:29 AM.

                Comment


                  #9
                  Hi PatrickH,

                  Thank you for your reply. The code samples you reference are fine but not really good examples of efficient use of DX resources. A better reference from your (NT) documentation might be in the Best Practices for SharpDX resources section.


                  Nonetheless, I come back to two things which I have verified:
                  1. In R13 of NT OnRenderTargetChanged() is called AFTER State.Configure. In R14 it has been reversed.

                  2. Can you please provide me with a snippet of how to reuse DX brushes and strokes with OnRenderTargetChanged(). There are a number of sample codes on NT that will no longer work.

                  EDIT: Are you saying then that I should set my brushes and strokes inside State.Defaults rather than State.Configure?

                  Comment


                    #10
                    Hello Zeos6,

                    Thank you for your response.
                    Originally posted by Zeos6 View Post
                    The code samples you reference are fine but not really good examples of efficient use of DX resources. A better reference from your (NT) documentation might be in the Best Practices for SharpDX resources section.
                    Are you stating there should be examples at the link below to demonstrate the proper use?
                    Originally posted by Zeos6 View Post
                    Can you please provide me with a snippet of how to reuse DX brushes and strokes with OnRenderTargetChanged(). There are a number of sample codes on NT that will no longer work.
                    Attached is an example using your code snippets, but can you advise which examples no longer work?
                    Originally posted by Zeos6 View Post
                    Are you saying then that I should set my brushes and strokes inside State.Defaults rather than State.Configure?
                    That is correct, please see the attached example based on your code snippets.
                    Attached Files

                    Comment


                      #11
                      Hi PatrickH,

                      It is hard to eat crow but here it goes...

                      I owe you an apology. For some reason I had it fixed in my brain that brush set ups were best done in State.Configure and I completely missed the fact that your examples did the initialization of brushes in State.Defaults. For some strange reason I saw in some examples that the brushes were properties set by the user and that is why I though this did not apply to internally set brushes. The fact that OnRenderTargetChanged() executed after State.Configure in the prior releases of NT just propagated this error on my part. D'oh.

                      I get it. Of course it's my error. Thank you for your help and patience in setting me straight. I appreciate it. I figured a good way to handle this this morning and your sample code just confirmed it for me. Thank you.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      670 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      379 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      111 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      575 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      582 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X