2

I come to ask because i'm new using windows forms and i need some help.

I have a Windows Forms project with 2 forms. The MainForm and the InnerForm.

I'm trying to access to a TableLayoutPanel in MainForm from InnerForm to add new rows in this tablelayoutpanel with some actions that happen in the InnerForm.

I have the next code:

MainForm:

 public partial class MainForm : Form
 {
    public MainForm()
    {
        InitializeComponent();

        TableLayoutPanel panel = tableLayoutPanel1;

        panel.ColumnCount = 4;
        panel.RowCount = 1;
        panel.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
        panel.Controls.Add(new Label() { Text = "Tag/ID" }, 0, 0);
        panel.Controls.Add(new Label() { Text = "Tipo" }, 1, 0);
        panel.Controls.Add(new Label() { Text = "Acción" }, 2, 0);
        panel.Controls.Add(new Label() { Text = "Ejecutar" }, 3, 0);
    }

    private void AddInnerForm(string url)
    {
        var inner = new InnerForm(url)
        // more code
    }

    public void agregarRow(ConsoleMessageEvents args){
        // some action with tableLayoutPanel1
    }
 }

InnerForm:

 public partial class InnerForm : UserControl
 {
    MainForm theMain;

    public InnerForm(MainForm main)
    {
        theMain = main;
    }

    public InnerForm(string url)
    {
        InitializeComponent();
        // more code
    }

    private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
    {
       theMain.agregaRow(args);
    }
 }

but when I'm debugging the program I get this error:

An unhandled exception of type 'System.NullReferenceException' occurred in Project.exe

Additional information: Object reference not set to an instance of an object.

in this line in InnerForm:

private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
{
   **theMain.agregaRow(args)**;
}

What is the problem here?

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
  • possible duplicate of [What does "Object reference not set to an instance of an object" mean?](http://stackoverflow.com/questions/779091/what-does-object-reference-not-set-to-an-instance-of-an-object-mean) – Dour High Arch Mar 27 '15 at 16:12
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – C-Pound Guru Mar 29 '15 at 18:44

2 Answers2

2

That should fix it. The field theMain was not initialized, also you need to call InitializeComponenets in every constructos to create the child controls.

   public partial class InnerForm : UserControl
 {
    MainForm theMain;

    public InnerForm(MainForm main)
    {
        InitializeComponent();
        theMain = main;
    }

    public InnerForm(string url, MainForm mainForm)
    {
        this.theMain = mainForm;
        InitializeComponent();
        // more code
    }

    private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
    {
       theMain.agregaRow(args);
    }
 }

And in MainForm:

private void AddInnerForm(string url)
    {
        var inner = new InnerForm(url, this)
        // more code
    }
Radin Gospodinov
  • 2,313
  • 13
  • 14
1

From your code it easy to see what you're initializing theMain variable in this constructor:

public InnerForm(MainForm main)
{
    theMain = main;
}

But in your code var inner = new InnerForm(url) you're using another constructor which is not initializing this variable - so theMain is left null and you're getting that exception when trying to access its method.

public InnerForm(string url)
{
    InitializeComponent();
    // more code
}
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71