Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

How to create a Custom Class?

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

    How to create a Custom Class?

    Hi!
    I'v tried to create a custom class "Top_Bottom" for my indicator. The code compiles. But when I add the indicator to a chart i get the error message "Object reference not set to an instance of an object."
    I'v identified the line in the code below that causes the error. But I'm not sure how to adjust the code. Is it sufficient to add the variables (double v, double l, int b) like I'v done in the class or do I have to add them to the Variabels region and Properties region too?

    Code:
    #region Using declarations
    
    using System.Collections.Generic;
    
    #endregion
    
    
        { 
            #region Variables
    
            private double x;
            private double y;
            
            private List< Top_Bottom  > DTlist;
            
            #endregion
    
            
            protected override void Initialize()
            {
                List< Top_Bottom  > DTlist = new List< Top_Bottom  >();
            }
    
           
            protected override void OnBarUpdate()
            {                    
                // This line causes the error. Add DT to dynamic list
               [B][COLOR=Red] DTlist.Add(new Top_Bottom(x, y, CurrentBar));  [/COLOR][/B]                          
            }
            
            // My Custom Class
            public class Top_Bottom
            {
                // Constructor
                public Top_Bottom(double v, double l, int b)
                {
                    this.Value = v;
                    this.Line = l;
                    this.BarNo = b;
                }
    
                public double Value { get; set; }
                public double Line { get; set; }
                public int BarNo { get; set; }
            }
    /Best regards

    #2
    Hi poseidon_sthlm,

    Creating a custom array list is, unfortunately, something I cannot provide support for.

    However, as a hint, you will need to ensure the array or any object within is not empty, when you try to access/call it.
    TimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Conceptzx, 10-11-2022, 06:38 AM
    3 responses
    60 views
    0 likes
    Last Post NinjaTrader_SeanH  
    Started by f.saeidi, Today, 01:32 PM
    1 response
    2 views
    0 likes
    Last Post NinjaTrader_Erick  
    Started by mmckinnm, Today, 01:34 PM
    0 responses
    3 views
    0 likes
    Last Post mmckinnm  
    Started by traderqz, Today, 12:06 AM
    9 responses
    16 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by kevinenergy, 02-17-2023, 12:42 PM
    117 responses
    2,766 views
    1 like
    Last Post jculp
    by jculp
     
    Working...
    X