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 argusthome, 03-08-2026, 10:06 AM
    0 responses
    72 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    44 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    25 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    30 views
    0 likes
    Last Post TheRealMorford  
    Started by Mindset, 02-28-2026, 06:16 AM
    0 responses
    61 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Working...
    X