C# TargetInitationException -(不应该存在?)

发布于 2024-12-01 10:19:21 字数 691 浏览 2 评论 0原文

我正在尝试在 WPF 中制作一个简单的应用程序,但遇到了一些异常情况。我有 2 个类:一个部分类(用于 WPF 窗口),另一个是我自己设置的公共类。当我尝试访问从 WPF 窗口类创建的类时,我遇到了 TargetInitationException ,告诉我对象引用未设置为对象的实例。但是,导致异常的对象引用被设置为对象的实例。

这是我的代码:

public partial class MainWindow : Window
{
    CurrentParent CP = new CurrentParent();
    public MainWindow()
    {
        InitializeComponent();
        CP.Par.Add("MainCanvas");
    }
}

public class CurrentParent
{
    private List<string> _Par;

    public List<string> Par
    {
        get { return _Par; }
        set { _Par = value; }
    }
}

当然,这是在一个名称空间中。我看不出为什么会出现此错误,因为我的对象引用 CP 显然是 CurrentParent 的实例。

有人知道如何解决这个问题吗?提前致谢!

-伊恩

I am trying to make a simple app in WPF, and i've run into a bit of an anomaly. I have 2 classes: a partial class (for the WPF Window), and another public class I have set up myself. When I try to access the class I created from the WPF window class, I run into a TargetInvocationException telling me the object reference is not set to an instance of an object. However, the object reference that results in the exception is set to an instance of an object.

Here's my code:

public partial class MainWindow : Window
{
    CurrentParent CP = new CurrentParent();
    public MainWindow()
    {
        InitializeComponent();
        CP.Par.Add("MainCanvas");
    }
}

public class CurrentParent
{
    private List<string> _Par;

    public List<string> Par
    {
        get { return _Par; }
        set { _Par = value; }
    }
}

Of course, this is in one namespace. I cannot see any reason why I should be getting this error, as my object reference CP clearly is an instance of CurrentParent.

Would anybody have any idea of how to fix this? Thanks in advance!

-Ian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦冥 2024-12-08 10:19:21

CurrentParent 中,字段 _Par 从未初始化,因此 CP.Par 为 null。当框架尝试调用 Add 时,会引发异常。您需要初始化_Par

public class CurrentParent
{
    private List<string> _Par = new List<string>();

    public List<string> Par
    {
        get { return _Par; }
        set { _Par = value; }
    }
}

In CurrentParent the field _Par is never initialized and therefore CP.Par is null. The exception is thrown when the frameworks tries to call Add. You need to initialze _Par:

public class CurrentParent
{
    private List<string> _Par = new List<string>();

    public List<string> Par
    {
        get { return _Par; }
        set { _Par = value; }
    }
}
浪漫人生路 2024-12-08 10:19:21

您没有实例化 CurrentParent 类中的 _Par 成员。这应该可以解决您的问题:

public class CurrentParent
{
 public CurrentParent()
 {
  this.Par = new List<String>();
 }

 public List<String> Par { get; set; }
}

请注意,该示例使用自动属性。这是一个更详细的示例,可以更好地突出您的问题:

public class CurrentParent
{
 public CurrentParent()
 {
  this._Par = new List<String>();
 }

 public List<String> Par
 {
  get { return this._Par; }
  set { this._Par = value; }
 }

 private List<String> _Par;
}

You are not instantiating the _Par member in the CurrentParent class. This should solve your problem:

public class CurrentParent
{
 public CurrentParent()
 {
  this.Par = new List<String>();
 }

 public List<String> Par { get; set; }
}

Note that the sample uses automatic properties. Here is a more verbose sample that better highlights your problem:

public class CurrentParent
{
 public CurrentParent()
 {
  this._Par = new List<String>();
 }

 public List<String> Par
 {
  get { return this._Par; }
  set { this._Par = value; }
 }

 private List<String> _Par;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文