Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unhandled exception when changing the account in the ChartTrader tab

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Unhandled exception when changing the account in the ChartTrader tab

    I have an indicator that posts filled orders and position updates outside Ninja. When I change the Account in the ChartTrader, the indicator is supposed to unsubscribe and resubscribe for the Events from the selected account. It works, but sometimes, I get the following error that lets me change the account again. I am requesting someone to throw some light on this issue.

    If I try to debug using Visual Studio, the ninja process terminates and I am unable to debug.


    Error: Unhandled Exception: The Object reference is not set to an instance of an object.

    Code:
    protected override void OnBarUpdate()
    {
    //Add your custom indicator logic here.
    
    if (State == State.Realtime)
    {
    try
    {
    if (ChartControl != null)
    {
    if (account != null)
    {
    currAccountName = account.ToString();
    prevAccount = account;
    //NinjaTrader.Code.Output.Process(string.Format("{0} : OBU | CurrentAccount {1}", Time[0], currAccountName.ToString()), PrintTo.OutputTab1);
    
    }
    ChartControl.Dispatcher.InvokeAsync((Action)(() =>
    {
    //You have to put the stuff below within this ChartControl.Dispatcher.InvokeAsync((Action)(() =>, because you are trying to access something on a different thread.
    accountSelector = Window.GetWindow(ChartControl.Parent).FindFirst("C hartTraderControlAccountSelector") as NinjaTrader.Gui.Tools.AccountSelector;
    //NinjaTrader.Code.Output.Process(string.Format("{0} : OBU Async | CurrentAccount {1}", Time[0], currAccountName.ToString()), PrintTo.OutputTab1);
    accountSelector.SelectionChanged += (o, args) =>
    {
    //NinjaTrader.Code.Output.Process(string.Format("{0} : OBU Async | SelectedAccount {1}", Time[0], accountSelector.SelectedAccount.ToString()), PrintTo.OutputTab1);
    if (accountSelector.SelectedAccount != null && currAccountName != accountSelector.SelectedAccount.ToString())
    {
    prevAccount.ExecutionUpdate -= OnExecutionUpdate;
    prevAccount.OrderUpdate -= OnOrderUpdate;
    prevAccount.PositionUpdate -= OnPositionUpdate;
    NinjaTrader.Code.Output.Process(string.Format("{0} : Unsubscribed {1}", Time[0], prevAccount.ToString()), PrintTo.OutputTab1);
    
    account = accountSelector.SelectedAccount;
    
    account.ExecutionUpdate += OnExecutionUpdate;
    account.OrderUpdate += OnOrderUpdate;
    account.PositionUpdate += OnPositionUpdate;NinjaTrader.Code.Output.Process(string.Format("{0} : Subscribed {1}", Time[0], account.ToString()), PrintTo.OutputTab1);
    currAccountName = account.ToString();
    }
    };
    }));
    }
    }
    catch (Exception ex)
    {
    NinjaTrader.Code.Output.Process(string.Format("OAU | {0}", ex.ToString()), PrintTo.OutputTab1);
    }
    Draw.TextFixed(this, "Account", "Account Selected: " + account.ToString(), TextPosition.BottomRight);
    }
    }

    #2
    Hello naanku,

    Thank you for your post.

    Object reference not set to an instance of an object indicates that there is a null value in a variable being used in your script.

    I recommend you debug the script to narrow now exactly which line your code is hitting this error at.

    You can use prints, place a print every few lines with a new number: Print("1"); etc. That will help you see where the script gets in execution when you hit the error. Once you know what line throws the error its much easier to debug. After you have confirmed which line is throwing the error, you can add another print to then check which variable or object is null.

    I am also attaching a sample script which detects a change in the chart trader selected account.

    Please let me know if I can assist further.
    Attached Files
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Have you tried using OwnerChart?

      It might make some of your code simpler.

      Good reading here.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by ricebun, Today, 07:37 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Started by wuannetraam, Today, 07:43 AM
      0 responses
      1 view
      0 likes
      Last Post wuannetraam  
      Started by sdauteuil, 09-23-2021, 10:16 AM
      9 responses
      1,253 views
      0 likes
      Last Post marcus2300  
      Started by KNalley, 01-24-2020, 10:33 AM
      8 responses
      961 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by Skifree, 05-07-2024, 11:47 PM
      3 responses
      30 views
      0 likes
      Last Post NinjaTrader_LuisH  
      Working...
      X