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

"Object reference not set" with respect to Array of custom data type

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

    "Object reference not set" with respect to Array of custom data type

    Hi there,
    This one is blowing my mind. I've created a data type using a class I named Event. I use it to mark an occurrence, and I want to track the bar at which it occurred and a state that indicates true or false. I'm using 1 or 0 so I can plot it on a separate pane when it occurs.

    My intention is to set priceAboveExtUpperHotZone[0].state = 0, and to eventually assign a bar value (priceAboveExtUpperHotZone[0].bar) at which an Event occurs. I am using an array so I can compare .state's 0-index with the 1-index so I know when it changes.

    Here's the relevant code:
    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
            public class HotZone2HotZone_Panel : Strategy
            {​
                    private Event[] priceAboveExtUpperHotZone = new Event[2];
                    private Event[] priceBelowExtUpperHotZone = new Event[2];​
    
                    protected override void OnBarUpdate()
                    {
                        if (CurrentBar < 20)
                            return;​
    
                        Print(182);
                        Print("Length of array: " + priceAboveExtUpperHotZone.Length);
                        priceAboveExtUpperHotZone[0].state = 0;
                        Print(185);
                    }
    
                    private class Event
                    {
                            public int state = 0;
                            public int bar = 0;​
                    }​
    ​
            }
    }
    In my output window, it reads:
    182
    Length of array: 2
    Strategy 'HotZone2HotZone_Panel': Error on calling 'OnBarUpdate' method on bar 20: Object reference not set to an instance of an object.

    I am yet to figure this out... It compiles just fine. When I go to assign a value to the array's field state, it seems to get angry. Does anyone see anything that is simply a no-no for what I'm doing above?

    Please let me know if you need any additional information. Thank you for your help in advance!!

    Nathan.

    #2
    Hello Nate G,

    An important note about this code before going into the error, Event is an existing C# type. I would suggest naming your custom class with a custom name like MyEvent to avoid issues with exports or compiling.

    Object reference not set means the object you tried to use was null.

    The problem is that you never create an object to use, you just tried to use the array with no objects in it. The array has a length because you made it have a length but that does not mean anything is in the array.

    This code is not valid as it is now
    Code:
    priceAboveExtUpperHotZone[0].state = 0;
    You would have first had to create an item:

    Code:
    priceAboveExtUpperHotZone[0] = new Event();
    //and then after that you could use the item you created:
    priceAboveExtUpperHotZone[0].state = 0;

    JesseNinjaTrader Customer Service

    Comment


      #3
      Excellent. Thank you, Jesse. I will play around with this this weekend.

      Nathan.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by rbeckmann05, Yesterday, 06:48 PM
      1 response
      12 views
      0 likes
      Last Post bltdavid  
      Started by llanqui, Today, 03:53 AM
      0 responses
      6 views
      0 likes
      Last Post llanqui
      by llanqui
       
      Started by burtoninlondon, Today, 12:38 AM
      0 responses
      10 views
      0 likes
      Last Post burtoninlondon  
      Started by AaronKoRn, Yesterday, 09:49 PM
      0 responses
      15 views
      0 likes
      Last Post AaronKoRn  
      Started by carnitron, Yesterday, 08:42 PM
      0 responses
      11 views
      0 likes
      Last Post carnitron  
      Working...
      X