TargetInvocationException was unhandled "Exception has been thrown by the target of an invocation." issue in WPF

In WPF, most probably the issue occurs when we are accessing null object in the constructor of the code file of the XAML or If you are invoking "PropertyChanged" event without having null check inside the constructor.



Example 1:


 public MainWindow()
        {
            InitializeComponent();
            DataTable dt=null;
            DataRow dr = dt.NewRow(); // Causes issue
         }

Example 2:
       string strInfoLog;
        public string InfoLog
        {
            get
            {
                return strInfoLog;
            }
            set
            {
                if (strInfoLog != value)
                {
                    strInfoLog = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("InfoLog")); // Reason for error since during the execution of constructor this is null.
                }
            }
        }



       public MainWindow()
        {
            InitializeComponent();

            InfoLog=""; //causes error

        }

   

No comments:

Post a Comment