Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Building a Class within...but getting the typical error: "Object reference..."

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

    Building a Class within...but getting the typical error: "Object reference..."

    Hi,
    When I try to run the Algo, the typical error pops up: "Object reference not set to an instance of an object"
    Posting here, just in case anyone in the forum might indicate what I'm doing wrong initializing the Class Trendline.
    Thanks a lot!

    Code:
    namespace NinjaTrader.Strategy
    {
        [Description("vvv")]
    
        public class Trendline
        {
            public int position;
            public double rate;
    
            public Trendline()  
            {
                position = 0;
                rate = 0.00;
            }
        }
    
        public class VVV : Strategy
        {
            #region Variables    
            private Trendline[]             uptrend = new Trendline[1];
            private Trendline[]           downtrend = new Trendline[1];      
            #endregion
    
            protected override void Initialize()
            {
                Do stuff
            }
            protected override void OnStartUp()
            {
                uptrend = new Trendline[activedays + 1];
                downtrend = new Trendline[activedays + 1];
            }
    
            protected override void OnBarUpdate()                                        
            {    
                if ( CurrentBar < 1 )
                {
                    for (int i = 0; i < activedays; i++)
                    {            
                        uptrend[i] = new Trendline();
                        downtrend[i] = new Trendline();
                    }
                    for (int i = 0; i < activedays; i++)
                    {            
                        uptrend[i].position = 0;
                        uptrend[i].rate = 0.00;
                        downtrend[i].position = 0;
                        downtrend[i].rate = 0.00;
                    }
                }
    Last edited by pstrusi; 06-18-2019, 05:08 PM.

    #2
    I've been able to solve the error by doing this modification:

    Code:
    namespace NinjaTrader.Strategy
    {
        [Description("vvv")]
    
        public class Trendline
        {
            public int position;
            public double rate;
    
            public Trendline()  
            {
                position = 0;
                rate = 0.00;
            }
        }
    
        public class VVV : Strategy
        {
            #region Variables    
            private Trendline[]             uptrend = new Trendline[1];
            private Trendline[]           downtrend = new Trendline[1];      
            #endregion
    
            protected override void Initialize()
            {
                Do stuff
            }
            protected override void OnStartUp()
            {
                uptrend = new Trendline[activedays + 1];
                downtrend = new Trendline[activedays + 1];
                for (int i = 0; i < activedays; i++)
                    {            
                        uptrend[i] = new Trendline();
                        downtrend[i] = new Trendline();
                    }
            }
    
            protected override void OnBarUpdate()                                        
            {    
                if ( CurrentBar < 1 )
                {
    
                    for (int i = 0; i < activedays; i++)
                    {            
                        uptrend[i].position = 0;
                        uptrend[i].rate = 0.00;
                        downtrend[i].position = 0;
                        downtrend[i].rate = 0.00;
                    }
                }

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Powerbucker, Yesterday, 05:09 PM
    2 responses
    13 views
    0 likes
    Last Post Powerbucker  
    Started by DerkWehler, 11-27-2024, 11:56 AM
    16 responses
    50 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by MikM45, Today, 03:10 AM
    2 responses
    9 views
    0 likes
    Last Post NinjaTrader_LuisH  
    Started by londonby, 10-31-2024, 03:09 PM
    2 responses
    42 views
    0 likes
    Last Post KenyaFuentes  
    Started by ezrollin, 02-26-2022, 11:14 PM
    14 responses
    305 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Working...
    X