Winform 应用程序中的 MDIparent 返回对象引用未设置为对象的实例
我对这段代码有疑问。您可以在此处找到所有类。
如果我启动应用程序并且想要打开一个新表单,我会收到此错误:
NullReferenceException:未将对象引用设置为对象的实例。
主应用程序设置为 isMDIcontainer = true;
现在我收到这部分代码的错误:
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。这是 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;
}
在同一个 sebsite 中,有人说可以通过以下方式解决此错误:
必须在接口中声明属性system.window.formparentForm
但我不明白如何。
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.
The main application is set to 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. This is the CreateIstance()
method:
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:
You must declare property system.window.form parentForm in interface
but I didn't understand how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
FileName
错误(文件名或路径不正确),CreateInstance
很可能返回null
。它返回
null
的结果是form
变量为null
并且对其进行任何成员访问(如form.MdiParent
将导致NullReferenceException
。请确保文件名正确并且该文件存在于搜索路径中。
Chances are good that
CreateInstance
is returning anull
because theFileName
is wrong (incorrect filename or path).The result of it returning a
null
is that theform
variable isnull
and any member access on it (as inform.MdiParent
will result in aNullReferenceException
.Make sure that the filename is correct and that the file exists in the path it is searched on.