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
         }
    }
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Radano, 06-10-2021, 01:40 AM
    20 responses
    615 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by Mizzouman1, Today, 07:35 AM
    0 responses
    4 views
    0 likes
    Last Post Mizzouman1  
    Started by i019945nj, 12-14-2023, 06:41 AM
    6 responses
    66 views
    0 likes
    Last Post i019945nj  
    Started by aa731, Today, 02:54 AM
    1 response
    8 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by BarzTrading, Today, 07:25 AM
    0 responses
    3 views
    0 likes
    Last Post BarzTrading  
    Working...
    X