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

    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 argusthome, 03-08-2026, 10:06 AM
      0 responses
      88 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      48 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      30 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      34 views
      0 likes
      Last Post TheRealMorford  
      Started by Mindset, 02-28-2026, 06:16 AM
      0 responses
      68 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Working...
      X