Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Object reference not set to an instance...
Collapse
X
-
Object reference not set to an instance...
I'm getting Object reference not set to an instance of an object errors in my code. I'm just now trying to understand advanced order handling, so maybe I've done something wrong. Any chance I can send this to support and you could take a look?Tags: None
-
trend747, this error occurs mainly when you try to access an object that doesn't exist, or when you try to access an object's properties while that object is null (which means the properties don't exist). You can pepper your script with Print() statements to see exactly where the error is occurring and once you've narrowed it down we can help out.
The idea is when your code is running and then throws this error, you can see exactly where it stopped by the last printed statement.Code:Print("placing order") longEnter1 = EnterLong(...); Print("order placed, checking some order property"); if (longEnter1.Name == "some name") { Print("name matches some name, thus the object reference works fine here and the name matches"); // do something } Print("trying something else next");AustinNinjaTrader Customer Service
-
Also, please take a look at the reference sample that shows how to work with using OnExecution() and OnOrderUpdate() to set protective orders - http://www.ninjatrader.com/support/f...ead.php?t=7499, as well as the help guide page for IOrders - http://www.ninjatrader.com/support/h...nt7/iorder.htm.AustinNinjaTrader Customer Service
Comment
-
I've narrowed it down but I'm still perplexed...
Code:private IOrder shortStopOrder = null;
Note that when I click on Enable for my strategy, it instantly gives me that error. It doesn't even get to any of the Print statements. If I comment out the order section, it works... so it's something to do with the above code.Code:protected override void OnBarUpdate() { if (Position.MarketPosition == MarketPosition.Flat && shortStopOrder == null) { SetStopLoss(CalculationMode.Ticks, SL); if (Close[1] > Close[2]) { Print("Placing short order..."); shortStopOrder = EnterShortStop(positionSize, Low[1] - 10 * TickSize, "mrtickles"); if (shortStopOrder.Name == "mrtickles") { Print("It works!"); } } else if (High[0] > High[1]) { CancelOrder(shortStopOrder); } } }
The error says Error on calling "OnBarUpdate" method... so as soon as it tries to call it, it errors out...Last edited by trend747; 07-15-2011, 10:10 AM.
Comment
-
Here's a stripped down version that still errors out.Attached Files
Comment
-
Regardless, you are still missing your initial bars escape statement. You need a:
statement at the beginning of your OnBarUpdate() method, or it means that, at the start, you are trying to access bars that do not yet exist.Code:if (CurrentBar < 2) return;
Comment
-
You know what, you're right, because we're calling it in a MarketPosition.Flat && shortStopOrder = null situation... you can't be flat in that case.Originally posted by NinjaTrader_Austin View Posttrend747, I strongly believe it is this line causing the error
because it is executed only if shortStopOrder is null (and the other conditions are true), and you can't cancel a null order.Code:else if (High[0] > High[1]) { CancelOrder(shortStopOrder); }
So I'll move that over to my exit code...
Thanks a lot man, appreciate it!
Comment
-
trend, it is really up to you how you want to cancel it. You could do something like this:
There are many different things you need to keep in mind when doing IOrders, like all of the possible order states, when to cancel the orders, when to reset the orders to null, etc.Code:if (shortStopOrder != null && (shortStopOrder.OrderState == OrderState.Working || shortStopOrder.OrderState == OrderState.Accepted) && cancelConditions == true) { CancelOrder(shortStopOrder); }
The reference sample I linked to earlier should go over most of these concepts.AustinNinjaTrader Customer Service
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
673 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
379 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
111 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
577 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
582 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment