Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

An object reference is required for the non-static field, method, or property

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

    An object reference is required for the non-static field, method, or property

    I have a file in AddOns, that defines some methods I want to use in my indicators

    From NT website?
    Warning: If a partial class is saved in one of the folders used for specific NinjaScript objects other than AddOns (e.g., Indicators folder), auto-generated NinjaScript code may be appended to the end of the class by the NinjaScript Editor when compiled, which will cause a compilation error. Saving these files in the AddOns folder will ensure they are still accessible and will not generate code which may be cause conflicts.

    Note: Methods within partial classes should be use the "public" and "static" modifiers, to allow for any other classes to invoke the methods without requiring an instance of the partial class.

    As soon as I add static to
    Code:
    public[B] static [/B]string DrawObject(string DrawObjectTag)
    I get the following error:
    An object reference is required for the non-static field, method, or property 'NinjaTrader.Gui.NinjaScript.IndicatorRenderBase.D rawObjects.get'

    Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    //This namespace holds Add ons in this folder and is required. Do not change it.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public partial class Indicator
         {
            public string MyDrawObject(string DrawObjectTag)
            {
                foreach (DrawingTool draw in DrawObjects.ToList())
                {
                    if (draw.Tag == DrawObjectTag)
                    {
                        return DrawObjectTag;
                    }
                }
                return null;
            }
         }
    }
    Any ideas would be appreciated very much. Thanks.

    #2
    Hello td_910,

    This would be outside of what is supported by NinjaTrader support, but I have an example you may find helpful.
    https://ninjatrader.com/support/forum/forum/historical-beta-archive/version-8-beta/83018-partial-class-does-not-work?p=712245#post712245

    This thread will remain open for any community members that would like to assist.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      thanks for your answer

      a little bit background, why I NEED TO USE this AddOn as a a workaround for a feature/bug in NT8
      in NT7 all my >20 indicators using tons of null checks and never throw an exception

      it's because the NT8 NinjaScript null check isn't thread safe
      also I do not get, what a not thread safe null check is for... :-((, it's useless


      here you go: https://www.screencast.com/t/ZIRh1dh3
      this nice exception occurred in a one line script on reload all historical data, while there are three other indicators loaded

      I'm trying to release five indicators on the Ecosystem, but don't want to start without the NT8 versions
      the NT7 versions are working flawless since a while, the only problem, that's left is THIS one
      Last edited by td_910; 04-15-2019, 12:19 AM.

      Comment


        #4
        Hello td_910,

        Making the DrawObject collection in one script static does not make it safe to access from other threads.

        Accessing methods from an indicator from within that indicator is thread safe.

        Accessing a different thread is not thread safe.

        The error you are experiencing is likely resulting when reloading the script.

        This issue 'DrawObjects collection is empty in OnBarUpdate when refreshing a script' is being tracked with ID# NTEIGHT-10987.

        This is caused because Reloading NinjaScripts will reload indicators as well as drawing tools (all ninjascripts). The DrawObjects collection no longer holds the old instances, but neither does it hold the new instances yet.

        A note was added to the help guide about this:

        "When reloading NinjaScript, all objects (including manual drawing tools) are reloaded at the same time. There is no guarantee a manually drawn object will be added to the DrawObjects collection before an indicator starts processing data."

        https://ninjatrader.com/support/help...rawobjects.htm


        The workaround would be waiting for the collection to be updated before checking the collection such as waiting for the first real-time bar update.

        A more complex (and undocumented) solution would be to modify the drawing tool to create an event when the property changes and have the indicator loop through the collection

        https://ninjatrader.com/support/foru...423#post677423


        We are tracking demand for creating an event that triggers when drawing object changes states so that this undocumented approach is not necessary with ID# SFT-1358.

        I will add your vote to this.
        Last edited by NinjaTrader_ChelseaB; 04-15-2019, 07:22 AM.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks for your comprehensive explanation.

          I explained what I want to do here https://ninjatrader.com/support/foru...93#post1054393

          The key for all my indicators is to check back for draw objects (with a null check) which was never a problem in NT7.
          This has to be done on historical data too, otherwise one is never able to see/test signals on historical price data.
          this is some example code to show what I want to do

          Code:
                          #region 2nd 1/2/3 tF
                          if (xTF==1 || xTF==2 || xTF==3)
                              for (int i = 2; i <= 4; i++)
                              {
                                  if (DrawObjects[xTF.ToString("N0")+"tF_Long" + (CurrentBar-i)] != null)
                                  {
                                      dynamic Prior_xtF_Text = DrawObjects[xTF.ToString("N0")+"tF_Long" + (CurrentBar-i)];
          
                                      if (Prior_xtF_Text.ToString().Equals("NinjaTrader.NinjaScript.DrawingTools.Text"))
                                          if (Prior_xtF_Text != null)
                                          {
                                              if (Body()[0]>0 && !(Prior_xtF_Text.DisplayText.Contains("2nd")) && !(xtF_Long.Contains("fEB")) && Low[i] < Low[0] && CountIf(delegate {return Low[2] > Low[1];}, i-1) <1)
                                              {
                                                  Draw.Text(this, xTF.ToString("N0")+"tF_Long"+CurrentBar, true, "2nd\n"+xtF_Long, 0, Low[0], -offset, fontColor, myFontSmallBold, TextAlignment.Center, Brushes.Transparent, ChartControl.Properties.ChartBackground, 100);
                                              }
                                          }
                                  }
                                  if (i>=Bars.BarsSinceNewTradingDay && Bars.BarsType.IsIntraday) break;
                              }
                          #endregion

          And one funny thing, this code has never thrown an exception, its in all my indicators

          Code:
                      else if (State == State.Configure)
                      {
                          //Label PriceActionIndicators
                          if (ChartControl != null && DrawObjects["PriceActionIndicators"] == null)
                              Draw.TextFixed(this, "PriceActionIndicators", "PriceActionIndicators.com", TextPosition.BottomLeft, ChartControl.Properties.ChartText,
                                ChartControl.Properties.LabelFont, Brushes.Transparent, Brushes.Transparent, 0);
                      }

          Last edited by td_910; 04-15-2019, 08:17 AM.

          Comment


            #6
            Hello td_910,

            Please refrain from creating multiple posts on the forums on the same topic, and also please refrain from emailing platformsupport [at] ninjatrader [dot] multiple times on the same topic, or emailing platform support and posting an identical post on the forums.

            This causes multiple members of our support to spend resources working on the same inquiry and may cause each technician to start at the beginning of reviewing your inquiry, tying up resources with our platform support and delaying our ability to respond to all customers in a timely manner.

            You are correct, with NinjaTrader 7 drawing objects are not NinjaScripts and are not affected by this behavior. This is a new behavior with NinjaTrader 8 as support for drawing tools was added by changing the drawing tool types to NinjaScripts.

            That said, as you already have a forum post open about this, I will allow Chris to continue assisting you in the other forum thread.
            Last edited by NinjaTrader_ChelseaB; 04-15-2019, 09:47 AM.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thanks for your help!
              I'm just a bit frustrated, that there is so much involved migrating code from NT7 to NT8

              Comment


                #8
                I created multiple posts, because I didn't get any answers.

                I still do not have an answer, regarding the DrawObjects.ToList() https://www.screencast.com/t/bQaMjjQJLz
                and if I have to use that for ANY null check of ANY object type regardless of user drawn or not in OnBarUpdate

                I'm no programmer, that's why I was a bit confused reading this in the code breaking changes:
                World's leading screen capture + recorder from Snagit + Screencast by Techsmith. Capture, edit and share professional-quality content seamlessly.


                for sure that method cannot be static, because then I have the same problem, of being not thread safe

                because, that is what I want to achieve with that workaround of a shared method I posted a bit further up
                having a thread safe DYNAMIC list copy each time I'm doing a null check without getting an exception

                Code:
                namespace NinjaTrader.NinjaScript.Indicators
                {
                    public partial class Indicator
                    {
                          // Loops through the DrawObjects collection via a threadsafe list copy
                        public string DrawObjectCheck(string DrawObjectTag)
                        {
                            foreach (DrawingTool draw in DrawObjects.ToList())
                            {
                                if (draw.Tag == DrawObjectTag)
                                {
                                    return DrawObjectTag;
                                }
                            }
                            return null;
                        }
                    }
                }
                Thanks
                Last edited by td_910; 04-15-2019, 11:50 AM.

                Comment


                  #9
                  Hello td_910,

                  As you have another thread open on this topic, I will allow Chris to continue assisting you in the other thread.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Sure, thanks.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    596 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    343 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    103 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    556 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    554 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X