-3

This is a pseudo-problem. I can force the link to open the desired page in the EventHandler, but I want to know what I am doing wrong in this scenario, and do it the right way:

In InitializeComponent(), in 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;

The EventHandler, LinkClicked, in public partial class Form1 : Form:

private void LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
}

When you click linkLabel1, you get the classic "NullReferenceException: Object reference not set to an instance of an object" when you call System.Diag....

I haven't had much training on error-handling or vocab, so this might as well be Greek to me. The button is not static... should I create a... new instance of the link?

Didn't the program already create an instance? If not, why does .Size, .Name, .Text, etc work... but not .Links.Add? No idea what I'm talking about.

Tako M.
  • 295
  • 1
  • 3
  • 13
  • -1 Visual Studio is awesome (I should know, I'm currently using Eclipse again) .. and one reason it is awesome is because **VS has a good debugger; use it**. That is, stop on when the Exception is thrown and then *inspect* the data/variables. I gave the downvote because learning the tools available will save you (and us) time and will reduce the rate at which the number of extremely localized questions grows .. perhaps not appreciably so :( –  Jan 10 '13 at 23:52

2 Answers2

4

The problem is probably in the e.Link.LinkData.ToString()

Check that LinkData is not null...

Blachshma
  • 17,097
  • 4
  • 58
  • 72
  • Checking if null simply prevents the exception from happening... I'm looking to remove the exception. – Tako M. Jan 10 '13 at 23:57
  • @TakoM. -That makes no sense... You get that exception because *you did something wrong*, you tried to access a null value. If you want to access the LinkData and have data inside, make sure you don't populate it by manually changing the `designer.cs` file – Blachshma Jan 11 '13 at 00:00
  • I probably should have mentioned I am using VS and made my question a little bit clearer - the error is clearly in the `e.Link.LinkData.ToString()`, and to me it is obvious where/why it was happening. The solution, however, was not obvious. That was what I was looking for, and what Eve delivered. No problem though. Thanks. – Tako M. Jan 11 '13 at 00:20
1

I believe that your problem is that you're adding the link in the auto-generated designer file. You can't predict when Visual Studio will modify/recreate it, and what changes it will have. In my case, the same statement you use gets replaced by:

this.linkLabel1.LinkArea = new System.Windows.Forms.LinkArea(1, 1);

The workaround is simple enough. Place:

this.linkLabel1.Links.Add(1, 1, "www.google.com");

In the Load event of your form. Also make sure to remove from the designer region all the unwanted parts of the code which have been refactored by Visual Studio.

e_ne
  • 8,340
  • 32
  • 43