开发者

WPF NullRefrence exception within InitializeComponent() method

开发者 https://www.devze.com 2023-04-12 20:30 出处:网络
I have a problem with the WPF, Framework 4.0 Profile and Intel HD onboard video cards. The NullReference exce开发者_高级运维ption occures in InitializeComponent() method when a new Window is created.

I have a problem with the WPF, Framework 4.0 Profile and Intel HD onboard video cards. The NullReference exce开发者_高级运维ption occures in InitializeComponent() method when a new Window is created.

    public override void Execute(object parameter)
    {
        InfoPage infoPage = new InfoPage(Owner);
        infoPage.ShowDialog();
    }

The exception occures because the infoPage variable is NULL. The behaviour is the same when I am trying to initialize other windows.

The Execute method is called when the user hits the "InfoPage" button. The class containing the execute methods inherited ICommand base class.

internal class InfoPageCommand : UICommand
{
    #region Public Methods

    public override void Execute(object parameter)
    {
        InfoPage infoPage = new InfoPage(Owner);
        infoPage.ShowDialog();
    }

    #endregion
}


Same thing happened to me. I turned on the CLR Exceptions as told by @townsean. Then the debug showed where the exception occur (It didn't show the debug point before). It was a ValueChanged event handler of a Slider control. I was accessing some other controls directly inside its code. Therefore I had to add a null check for those controls.

For me the error started to appear after i changed the default value of the slider. (It seems logical for the event handler to be called when it does).

private void slAddRemove_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    if (tbAdd != null && sender.Equals(slAdd))
    {
        tbAdd.Text = slAdd.Value.ToString();
    }

    if (tbRemove != null && sender.Equals(slRemove))
    {
        tbRemove.Text = slRemove.Value.ToString();
    }
}

After adding the tbAdd != null and tbRemove != null the error didn't occur.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号