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 tkaboris, Today, 05:13 PM
    0 responses
    2 views
    0 likes
    Last Post tkaboris  
    Started by GussJ, 03-04-2020, 03:11 PM
    16 responses
    3,281 views
    0 likes
    Last Post Leafcutter  
    Started by WHICKED, Today, 12:45 PM
    2 responses
    19 views
    0 likes
    Last Post WHICKED
    by WHICKED
     
    Started by Tim-c, Today, 02:10 PM
    1 response
    10 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by Taddypole, Today, 02:47 PM
    0 responses
    5 views
    0 likes
    Last Post Taddypole  
    Working...
    X