0

I'm busy with a project in C# that involves 2 Windows Form. The program asks to user to enter a topic in a textbar and then the user selects from 3 websites to search and display a video.

This is the code from the first form,

private void btnSearch_Click(object sender, EventArgs e)
{
        //Assign Search
        Search = SearchBox.Text;
        //Assign Website
        if (rb1YouTube.Checked)
        {
            WebSite = 1;
        }
        else if (rb2MetaCafe.Checked)
        {
            WebSite = 2;
        }
        else if (rb3Yahoo.Checked)
        {
            WebSite = 3;
        }
        //Assign Category
        Category = " - " + cbCategory.Text;
        //Input Validation
        if (Search == "" || Category == " - " || WebSite == 0)
        {
            System.Windows.Forms.MessageBox.Show("All fields require values");
        }
        else
        {
           //Declare class and pass variables
           Results RF = new Results(Search, WebSite, Category);  
           RF.Show();  
        }
   }

And here is the code from the second.

public Results(String Search, int WebSite, String Category)
{
    try
    {
        switch (WebSite)
        {
            case 1: webBrowser1.Navigate("https://www.youtube.com/");
                break;
            case 2: webBrowser1.Navigate("http://www.metacafe.com/");
                break;
            case 3: webBrowser1.Navigate("https://screen.yahoo.com/");
                break;
        }
    }
    catch(Exception E)          
    {
        MessageBox.Show(" " + E);
    }
    InitializeComponent();
}

The error occurs when the values are passed into the new form, after its run through the switch/case.

I have debugged the program and had a breakpoint on the public Results(String Search, int WebSite, String Category) line, I've ensured the values are being sent and received.

John Saunders
  • 160,644
  • 26
  • 247
  • 397

1 Answers1

4

Move InitializeComponent() method call before your switch. I suppose that webBrowser1 is not initialized yet and its value is null when you call its Navigate(...) method.

Oleksandr Zolotarov
  • 919
  • 1
  • 10
  • 20
  • 1
    This is your answer. That initialise components runs through your designer.cs class and initialises all the controls. So as that is running after your code, things like webBrowser1 will be null... Hence the NullReferenceException. Cut + Paste and fixed. – Mark McGookin May 26 '15 at 19:02
  • 1
    @MarkMcGookin Yep, exactly. – Oleksandr Zolotarov May 26 '15 at 19:04
  • `InitializeComponent` shouldn't be in the method *at all*, it should be in the constructor only. – Ron Beyer May 26 '15 at 19:24
  • @RonBeyer If you read the code carefully, you'll see that `public Results(String Search, int WebSite, String Category)` IS a constructor. – Oleksandr Zolotarov May 26 '15 at 19:27
  • Yes, you are right, I typically leave the default constructor and chain it when overriding, so I didn't notice it as one. – Ron Beyer May 26 '15 at 19:28
  • Thank you all so much for the help, and explaining your different points of view. This works ! And I learnt something new today :] –  May 27 '15 at 07:22