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 helpwanted, Today, 03:06 AM
    1 response
    12 views
    0 likes
    Last Post sarafuenonly123  
    Started by Brevo, Today, 01:45 AM
    0 responses
    9 views
    0 likes
    Last Post Brevo
    by Brevo
     
    Started by aussugardefender, Today, 01:07 AM
    0 responses
    5 views
    0 likes
    Last Post aussugardefender  
    Started by pvincent, 06-23-2022, 12:53 PM
    14 responses
    242 views
    0 likes
    Last Post Nyman
    by Nyman
     
    Started by TraderG23, 12-08-2023, 07:56 AM
    9 responses
    387 views
    1 like
    Last Post Gavini
    by Gavini
     
    Working...
    X