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 rtwave, 04-12-2024, 09:30 AM
    4 responses
    29 views
    0 likes
    Last Post rtwave
    by rtwave
     
    Started by yertle, Yesterday, 08:38 AM
    7 responses
    28 views
    0 likes
    Last Post yertle
    by yertle
     
    Started by bmartz, 03-12-2024, 06:12 AM
    2 responses
    21 views
    0 likes
    Last Post bmartz
    by bmartz
     
    Started by funk10101, Today, 12:02 AM
    0 responses
    6 views
    0 likes
    Last Post funk10101  
    Started by gravdigaz6, Yesterday, 11:40 PM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Working...
    X