Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Checking for Null References

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

    Checking for Null References

    Applies to NinjaTrader 7

    A common object-oriented programming error is not checking for null references on your object variables This will cause an “Object reference not set to an instance of an object” error.

    For example:
    You create a variable that holds an IOrder object
    Code:
    private IOrder entryOrder = null;
    But in the OnBarUpdate() method you do not check if this variable as been assigned an IOrder object thus when trying to access object properties it fails and yields the “Object reference not set…” error since the variable is null.

    Code:
    protected override void OnBarUpdate()
    {
         if (entryOrder.Filled > 0)
              // Do something
    }
    This will generate an error because you cannot access the object or any of its properties yet. You must always check if an object variable is null before attempting to access the object.
    Code:
    protected override void OnBarUpdate()
    {
         if (entryOrder == null)
         {
              entryOrder = EnterLong();
         } 
         else if (entryOrder != null)
         {
              if (entryOrder.Filled > 0)
                   // Do something
         }
    }
    Last edited by NinjaTrader_Jesse; 06-03-2015, 12:57 PM.
    Josh P.NinjaTrader Customer Service

    #2
    Applies to NinjaTrader 8

    A common object-oriented programming error is not checking for null references on your object variables This will cause an “Object reference not set to an instance of an object” error.

    For example:
    You create a variable that holds an Order object
    Code:
    private Order entryOrder = null;
    But in the OnBarUpdate() method you do not check if this variable as been assigned an Order object, thus when trying to access object properties it fails and yields the “Object reference not set…” error since the variable is null.

    Code:
    protected override void OnBarUpdate()
    {
         if (entryOrder.Filled > 0)
              // Do something
    }
    This will generate an error because you cannot access the object or any of its properties yet. You must always check if an object variable is null before attempting to access the object.

    Code:
     
    protected override void OnBarUpdate()
    {
         if (entryOrder == null)
         {
              entryOrder = EnterLong();
         } 
         else if (entryOrder != null)
         {
              if (entryOrder.Filled > 0)
                   // Do something
         }
    }

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by mlarocco, 06-20-2025, 11:12 AM
    1 response
    64 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by futurenow, 12-06-2021, 05:49 PM
    19 responses
    1,031 views
    0 likes
    Last Post Redders
    by Redders
     
    Started by mathfrick2023, 05-08-2025, 12:51 PM
    8 responses
    148 views
    0 likes
    Last Post Yogaman
    by Yogaman
     
    Started by several, 04-22-2025, 05:21 AM
    1 response
    302 views
    0 likes
    Last Post Lukasxgtx  
    Started by NTEducationTeam, 06-12-2025, 02:30 PM
    0 responses
    54 views
    0 likes
    Last Post NTEducationTeam  
    Working...
    X