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 sidlercom80, 10-28-2023, 08:49 AM
    166 responses
    2,234 views
    0 likes
    Last Post sidlercom80  
    Started by thread, Yesterday, 11:58 PM
    0 responses
    1 view
    0 likes
    Last Post thread
    by thread
     
    Started by jclose, Yesterday, 09:37 PM
    0 responses
    6 views
    0 likes
    Last Post jclose
    by jclose
     
    Started by WeyldFalcon, 08-07-2020, 06:13 AM
    10 responses
    1,414 views
    0 likes
    Last Post Traderontheroad  
    Started by firefoxforum12, Yesterday, 08:53 PM
    0 responses
    11 views
    0 likes
    Last Post firefoxforum12  
    Working...
    X