0

I write an application using MVVM pattern. My main view model have a property 'CurrentViewModel'. When I click top panel buttons (Account, Trade History etc...) CurrentViewModel property is updated. SpotViewModel is constantly getting the candles info from Binance api and updating UI in async method GetChartDataAsync() and everything works fine. But when I click any top panel button, for example Trade History, I get an exception "object reference not set to an instance of an object". Help please.

Here is The UI:

Spot View

Here is SpotViewModel constructor. It is called when I click 'Spot' button and it assigns the SpotViewModel to the CurrentViewModel property:

public SpotViewModel(List<UserLoginInfo> appUsers, string selectedExchange)
{
    SelectedExchange = selectedExchange;
    ExchangeUsers = appUsers;

    foreach (var user in ExchangeUsers)
    {
        AvailableExchanges.Add(user.Exchange);
    }

    Symbol24Stats.Symbol = "XRPUSDT";
}

Here is GetChartDataAsync method:

public async Task GetChartDataAsync(CancellationToken token)
{
    TimeFrame timeFrame = FancyCandles.TimeFrame.M5;
    CandlesSource candles = new CandlesSource(timeFrame);

    while (true)
    {
        if (token.IsCancellationRequested)
        {
            return;
        }
        List<TradingCommonTypes.ICandle> exchangeCandles = new List<TradingCommonTypes.ICandle>();

        try
        {
            exchangeCandles = AppUser.MarketInfo.GetCandles(Symbol24Stats.Symbol, "5m", 500);
        }

        catch { continue; }

        Application.Current.Dispatcher.Invoke(() =>
        {
            candles.Clear();
            foreach (var candle in exchangeCandles)
            {
                candles.Add(new Candle(candle.OpenTime, candle.Open, candle.High, candle.Low, candle.Close, candle.Volume));
            }
        });

        SpotCandles = candles;                
    }
}

Here is The command from MainViewModel which assigns the CurrentViewModel property:

public void OnSelectMainMenuTabCommandExecuted(object p)
{
    var activeTabColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#D81D3C"));

    switch (p as string)
    {
        case "Account":
            accountBackgroundWorker.DoWork += AccountBackgroundWorker_DoWork;
            accountBackgroundWorker.RunWorkerCompleted += AccountBackgroundWorker_RunWorkerCompleted;
            accountBackgroundWorker.RunWorkerAsync();
            break;
        case "History":
            cts.Cancel();

            tradeHistoryBackgroundWorker.DoWork += TradeHistoryBackgroundWorker_DoWork;
            tradeHistoryBackgroundWorker.RunWorkerCompleted += TradeHistoryBackgroundWorker_RunWorkerCompleted;
            tradeHistoryBackgroundWorker.RunWorkerAsync();
            break;
        case "Spot":
            tradeHistoryBackgroundWorker.DoWork += SpotBackgroundWorker_DoWork;
            tradeHistoryBackgroundWorker.RunWorkerCompleted += SpotBackgroundWorker_RunWorkerCompleted;
            tradeHistoryBackgroundWorker.RunWorkerAsync();
            break;
        case "Auto":
            AutoTradingTabBackground = activeTabColor;
            break;
        case "Futures":
            FuturesTabBackground = activeTabColor;
            break;
    }

    MainMenuSpinnerVisibility = Visibility.Visible;
    IsExchangeSelectionEnabled = false;
}

Calling of async task is happening in SpotBackgroundWorker_RunWorkerCompleted:

private void SpotBackgroundWorker_RunWorkerCompleted(object? sender, RunWorkerCompletedEventArgs e)
{
    SetMainTabsBackgroundToDefault();
    SpotTabBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#D81D3C"));
    MainMenuSpinnerVisibility = Visibility.Collapsed;
    IsExchangeSelectionEnabled = true;

    Task.Run(() => ((SpotViewModel)CurrentViewModel).GetChartDataAsync(cts.Token));

}
  • Calling Task.Run in a constructor is a severe flaw. – Clemens Aug 05 '22 at 18:57
  • @Clemens Looking at my code, can you advise where should I call Task.Run? Because I need this task to run imidietly when Spot view is opened – Александр Aug 05 '22 at 21:10
  • Ok, now I call this task, when Spot button is clicked. But it doesn't fix the problem. I could propbobly find the way to solve this problem. The thing is Visual studio doesn't show what property, or variable is Null. Ever running in debug doesn't help. The exception occurs when all the code completed – Александр Aug 05 '22 at 21:31

0 Answers0