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 cfruntgf, Today, 04:56 AM
    0 responses
    2 views
    0 likes
    Last Post cfruntgf  
    Started by Frank1000, Today, 03:05 AM
    0 responses
    3 views
    0 likes
    Last Post Frank1000  
    Started by ntdev, Today, 01:14 AM
    2 responses
    10 views
    0 likes
    Last Post ntdev
    by ntdev
     
    Started by ct, Today, 02:22 AM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Jason  
    Started by yodaler, Yesterday, 06:12 PM
    3 responses
    11 views
    0 likes
    Last Post yodaler
    by yodaler
     
    Working...
    X