In Form1.cs i have
public const int n = 30;
public TabPage[] tp = new TabPage[n];
private void toolStripSeparator1_Click(object sender, EventArgs e)
{
RenameFunc rf = new RenameFunc();
rf.ShowDialog();
}
In RenameFunc.cs i have
private void button1_Click_1(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
if (textBox1.Text != null)
/*Line 24 */ frm1.tp[Array.IndexOf(frm1.tp, frm1.tabControl1.SelectedTab)].Text = textBox1.Text;
Application.Exit();
}
tabControl1 is also seted tu Public
in Line 24 i get error
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Forms.TabControl.get_SelectedTabInternal()
at System.Windows.Forms.TabControl.get_SelectedTab()
at Notepad1._0.RenameFunc.button1_Click_1(Object sender, EventArgs e) in D:\C#\Notepad1.0\Notepad1.0\RenameFunc.cs:line 24
How to correct ?
I don’t know what
tpis, but, I’m sure it’s not initialized and this gives the exception.The reason is easily found in the previous line
here you create a new instance of Form1. You are not referencing the first Form1 from which your RenameFunc has been called.
Perhaps you could pass a reference to the correct Form1 when you call RenameFunc, for example
and keep that reference in your RenameFunc internal vars
}
and in button1_Click_1 use that reference instead of new Form1
However a little explanation on tp would be beneficial