【问题标题】:.net c++/CLI combobox valueMember nullReferenceException.net c++/CLI 组合框 valueMember nullReferenceException
【发布时间】:2011-12-05 12:05:14
【问题描述】:

我有一个实验课。我创建了这个类的一些实例并用这些对象填充组合框。我使用了 DisplayMember 和 ValueMember 属性。人口还可以,但是当我从组合框中读取 selectedValue 时,它​​给了我 NullReferenceException。

这是我的代码:

public ref class ABC
{
    ABC( Experiment^ exp )
    {
        this->exp = exp;
        this->name = this->exp->getName();
    }
    property Experiment^ Exp
    {
        Experiment^ get()
        {
            return this->exp;
        }
    }
    property String^ Name
    {
        String^ get()
        {
            return this->name;
        }
    }

    Experiment^ exp;
    String^ name;
}

 

Experiment^  e1;
this->combobox->Items(gcnew ABC(e1));
this->combobox->DisplayMember = "Name";
this->combobox->ValueMember = "Exp";

this->combobox->SelectedIndex = 0;

Experiment^  e2 = (Experiment^)(this->combobox->SelectedValue); // nullReferenceException

【问题讨论】:

  • 实际可以编译的邮政编码。将未初始化的 Experiment 对象传递给 ABC 构造函数是不明智的。否则,我看不到任何明显的方法来重现异常。
  • ComboBox::Items 是属性,不是方法;我同意 Hans,代码是假的。

标签: .net combobox c++-cli nullreferenceexception


【解决方案1】:

我不知道为什么,但是当我交换以下行时

Experiment^  e2 = (Experiment^)(this->combobox->SelectedValue);

用这条线

Experiment^  e2 = ((ABC^)(this->combobox->SelectedItem))->Exp;

没关系,这解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多