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

StackOverflow https://stackoverflow.com/questions/8371766

  •  27-10-2019
  •  | 
  •  

Question

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.

Was it helpful?

Solution

Chances are good that CreateInstance is returning a null because the FileName is wrong (incorrect filename or path).

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.

Make sure that the filename is correct and that the file exists in the path it is searched on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top