Announcement

Collapse
No announcement yet.

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;

    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 CaptainJack, 05-29-2026, 05:09 AM
      0 responses
      167 views
      0 likes
      Last Post CaptainJack  
      Started by CaptainJack, 05-29-2026, 12:02 AM
      0 responses
      88 views
      0 likes
      Last Post CaptainJack  
      Started by charlesugo_1, 05-26-2026, 05:03 PM
      0 responses
      128 views
      0 likes
      Last Post charlesugo_1  
      Started by DannyP96, 05-18-2026, 02:38 PM
      1 response
      208 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 05-11-2026, 05:56 AM
      0 responses
      185 views
      0 likes
      Last Post CarlTrading  
      Working...
      X