如果此方法中的邏輯是從事件處理程序進行操作,如Button_Click
它完美,而是從一個方法運行此如下面時,我得到的錯誤:未將對象引用設置爲對象的實例。 (C#)
hostView.SelectedNode.Nodes.Add(newNode);
Object reference not set to an instance of an object.
這裏是我的代碼:
private void SetupHostTree()
{
// Set internal host names
using (var reader = File.OpenText("Configuration.ini"))
{
List<string> hostnames = ParseInternalHosts(reader).ToList();
foreach (string s in hostnames)
{
TreeNode newNode = new TreeNode(s);
hostView.SelectedNode.Nodes.Add(newNode);
string title = s;
TabPage myTabPage = new TabPage(title);
myTabPage.Name = s;
tabControl1.TabPages.Add(myTabPage);
}
}
}
請使用調試器,並提供有關錯誤發生的地點的詳細信息。 – 2012-01-14 11:59:55
發生線路錯誤? – 2012-01-14 12:00:08
他給出了問題中的所有信息。仔細閱讀。 – 2012-01-14 12:05:25