【发布时间】:2013-01-10 23:47:12
【问题描述】:
这是一个伪问题。我可以强制链接在 EventHandler 中打开所需的页面,但我想知道在这种情况下我做错了什么,并以正确的方式做:
在 InitializeComponent() 中,在partial class Form1:
public void InitializeComponent()
{
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
//...
//
// linkLabel1
//
this.linkLabel1.AutoSize = true;
this.linkLabel1.LinkArea = new System.Windows.Forms.LinkArea(0, 1);
this.linkLabel1.LinkColor = System.Drawing.Color.Red;
this.linkLabel1.Location = new System.Drawing.Point(259, 100);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new System.Drawing.Size(13, 17);
this.linkLabel1.TabIndex = 5;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "?";
/**/
this.linkLabel1.Links.Add(1, 1, "www.google.com"); //This is the only code I have added manually.
/**/
this.linkLabel1.UseCompatibleTextRendering = true;
this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.LinkClicked);
}
//...
private System.Windows.Forms.LinkLabel linkLabel1;
public partial class Form1 : Form 中的 EventHandler,LinkClicked:
private void LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
}
当您单击linkLabel1 时,当您调用System.Diag... 时,您会得到经典的“NullReferenceException: Object reference not set to an instance of an object”。
我没有接受过太多错误处理或词汇方面的培训,所以这对我来说可能是希腊语。该按钮不是静态的...我应该创建一个...新的链接实例吗?
程序不是已经创建了一个实例吗?如果没有,为什么.Size、.Name、.Text 等工作......但不是.Links.Add?不知道我在说什么。
【问题讨论】:
-
-1 Visual Studio 很棒(我应该知道,我现在又在使用 Eclipse).. 它很棒的一个原因是因为 VS 有一个很好的调试器;使用它。也就是说,在抛出异常时停止,然后检查数据/变量。我投了反对票,因为学习可用的工具将节省您(和我们)的时间,并会减少极端本地化问题数量增长的速度.. 可能不是很明显:(
标签: c# winforms nullreferenceexception linklabel