Problem I can't get past.
-
I have an application it is tabbed based and holds a texteditor in each tab (The ICSharpCode TextEditorControl). I can open as many tabs as I want but when I switch bewteen 2 or three tabs I get this error. I'm am getting "Object reference not set to an instance of an object" result back. Problay somthing small I am missing somewhere, but I can't see where. Below is the code for when a tab switches.
void TabControl1SelectedIndexChanged(object sender, EventArgs e) { if(tabControl1.TabPages.Count > 0 && !tabControl1.SelectedTab.Disposing) { openFile = tabControl1.SelectedTab.ToolTipText; if(lengthSet == false) { if(oldLength != tabControl1.SelectedTab.Controls["textEditorControl1"].Text.Length) oldLength = tabControl1.SelectedTab.Controls["textEditorControl1"].Text.Length; if((bool)tabControl1.SelectedTab.Tag == true) { saveToolStripMenuItem.Enabled = false; toolStripButton3.Enabled = false; } else { saveToolStripMenuItem.Enabled = true; toolStripButton3.Enabled = true; } } } lengthSet = false; }
The Lineif((bool)tabControl.SelectedTab.Tag == true)
returns the error If I addTextEditorControl editor = (TextEditorControl)tabControl1.SelectedTab.Controls["textEditorControl1"];
and replace the line with the error withif(editor.IsReadOnly)
It still returns the same thing. -
I have an application it is tabbed based and holds a texteditor in each tab (The ICSharpCode TextEditorControl). I can open as many tabs as I want but when I switch bewteen 2 or three tabs I get this error. I'm am getting "Object reference not set to an instance of an object" result back. Problay somthing small I am missing somewhere, but I can't see where. Below is the code for when a tab switches.
void TabControl1SelectedIndexChanged(object sender, EventArgs e) { if(tabControl1.TabPages.Count > 0 && !tabControl1.SelectedTab.Disposing) { openFile = tabControl1.SelectedTab.ToolTipText; if(lengthSet == false) { if(oldLength != tabControl1.SelectedTab.Controls["textEditorControl1"].Text.Length) oldLength = tabControl1.SelectedTab.Controls["textEditorControl1"].Text.Length; if((bool)tabControl1.SelectedTab.Tag == true) { saveToolStripMenuItem.Enabled = false; toolStripButton3.Enabled = false; } else { saveToolStripMenuItem.Enabled = true; toolStripButton3.Enabled = true; } } } lengthSet = false; }
The Lineif((bool)tabControl.SelectedTab.Tag == true)
returns the error If I addTextEditorControl editor = (TextEditorControl)tabControl1.SelectedTab.Controls["textEditorControl1"];
and replace the line with the error withif(editor.IsReadOnly)
It still returns the same thing. -
Any chance that the SelectedTab is null when this exception is thrown? Check it with the debugger.
It shows that it selected has a TabPage I can view all the info for that tabpage and everything though the debugger. It's weird that it stops and that line though, it skips the rest of the places where I have used
tabControl1.SelectedTab
. -
I have an application it is tabbed based and holds a texteditor in each tab (The ICSharpCode TextEditorControl). I can open as many tabs as I want but when I switch bewteen 2 or three tabs I get this error. I'm am getting "Object reference not set to an instance of an object" result back. Problay somthing small I am missing somewhere, but I can't see where. Below is the code for when a tab switches.
void TabControl1SelectedIndexChanged(object sender, EventArgs e) { if(tabControl1.TabPages.Count > 0 && !tabControl1.SelectedTab.Disposing) { openFile = tabControl1.SelectedTab.ToolTipText; if(lengthSet == false) { if(oldLength != tabControl1.SelectedTab.Controls["textEditorControl1"].Text.Length) oldLength = tabControl1.SelectedTab.Controls["textEditorControl1"].Text.Length; if((bool)tabControl1.SelectedTab.Tag == true) { saveToolStripMenuItem.Enabled = false; toolStripButton3.Enabled = false; } else { saveToolStripMenuItem.Enabled = true; toolStripButton3.Enabled = true; } } } lengthSet = false; }
The Lineif((bool)tabControl.SelectedTab.Tag == true)
returns the error If I addTextEditorControl editor = (TextEditorControl)tabControl1.SelectedTab.Controls["textEditorControl1"];
and replace the line with the error withif(editor.IsReadOnly)
It still returns the same thing.SelectedTab must be null, unless tabControl is. That's the only explanation for the error. Assuming the error is not thrown by the cast, and the Tag might be null ?
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
SelectedTab must be null, unless tabControl is. That's the only explanation for the error. Assuming the error is not thrown by the cast, and the Tag might be null ?
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Thanks, I checked out the debugging info and saw that Tag was not even showing up in it, so I created another class to extend the TabPage and added an IsFileReadOnly variable and it seems to work fine now.
-
Thanks, I checked out the debugging info and saw that Tag was not even showing up in it, so I created another class to extend the TabPage and added an IsFileReadOnly variable and it seems to work fine now.
Great - that exception always means something is null, I didn't realise it would be thrown by the cast attempt, that occured to me at the last minute. I'd always use the Convert. or a TryParse ( bool.TryParse ? ) to do conversions, and check for null first.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )