[英]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
返回null
的FileName
很大。
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.