简体   繁体   English

Winform应用程序中的MDIparent返回对象引用未设置为对象的实例

[英]MDIparent in Winform Application return Object reference not set to an instance of an object

I have a problem with this code. 我对此代码有疑问。 You can find all classes here . 您可以在这里找到所有课程。

If I launch the application and I want open a new Form i receive this error: 如果我启动该应用程序并想打开一个新表格,则会收到此错误:

NullReferenceException : Object reference not set to an instance of an object. NullReferenceException:对象引用未设置为对象的实例。

The main application is set to isMDIcontainer = true; 主应用程序设置为isMDIcontainer = true;

Now I received the error in this part of code: 现在,我在这部分代码中收到错误:

private void PluginClick(object sender, EventArgs e)
{
    ToolStripMenuItem menu = (ToolStripMenuItem)sender;
    Plugin.PluginForm form = ((PluginInfo)menu.Tag).CreateInstance();
    form.MdiParent = this;   // Here is thrown the error
    form.Show();
}

Plugin.PluginForm is only an Extended Form. Plugin.PluginForm只是扩展形式。 This is the CreateIstance() method: 这是CreateIstance()方法:

public PluginForm CreateInstance()
{
    if (!File.Exists(FileName))
        return null;

    Assembly ass = Assembly.LoadFile(FileName);
    foreach (Type type in ass.GetTypes())
    {
        if (type.BaseType == typeof(PluginForm))
        {
           return (PluginForm)Activator.CreateInstance(type);
        }
    }
    return null;
}

In the same sebsite someone says that this error could may be resolved in this way: 在同一个sebsite中,有人说可以通过以下方式解决此错误:

You must declare property system.window.form parentForm in interface 您必须在接口中声明属性system.window.form parentForm

but I didn't understand how. 但我不知道如何。

Chances are good that CreateInstance is returning a null because the FileName is wrong (incorrect filename or path). 由于FileName错误(文件名或路径不正确), CreateInstance返回nullFileName很大。

The result of it returning a null is that the form variable is null and any member access on it (as in form.MdiParent will result in a NullReferenceException . 它返回null的结果是form变量为null并且对其进行任何成员访问(如form.MdiParent都会导致NullReferenceException

Make sure that the filename is correct and that the file exists in the path it is searched on. 确保文件名正确,并且文件存在于搜索路径中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 绑定数据表时,对象引用未设置为Winform中的对象实例 - Object reference not set to an instance of an object in winform when binding a datatable Winform自定义控件为什么“对象引用未设置为对象的实例”? - Winform Custom Control Why “Object Reference not set to an instance of an object”? 对象引用未设置为C#winform devexpress中的对象错误实例? - Object reference not set to an instance of an object error in C# winform devexpress? 初始化供整个winform使用的字段…(对象引用未设置为对象实例) - Initializing a field for an entire winform to use… (Object reference not set to instance of an object) 从winform打开wpf引发对象引用未设置为的实例 - open wpf from winform throws object reference not set to an instance of 对象引用未设置为实例 - object reference not set to an instance 为什么代码返回“对象引用未设置为对象实例”错误? - Why the codes return 'Object reference not set to an instance of an object' error? 错误返回新的 SearchInput。 Object 引用未设置为 object 的实例 - Error return new SearchInput. Object reference not set to an instance of an object 你调用的对象是空的 - Object reference not set to an instance of an object 对象引用未设置为对象#1的实例 - Object reference not set to an instance of an object #1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM