# C# Error

## Recommended Posts

agent 007    100
Hey! I'm learning C#, and I decided to start with a web browser. But I keep getting one error that is really bothering me. This is my code. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Halogen { public partial class Halogen : Form { public Halogen() { InitializeComponent(); } private void goButton_Click(object sender, System.EventArgs e) { "webBrowser1.Navigate(new Uri(comboBox1.SelectedItem.ToString()));" } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void backButton_Click(object sender, System.EventArgs e) { webBrowser1.GoBack(); } private void forwardButton_Click(object sender, System.EventArgs e) { webBrowser1.GoForward(); } private void stopButton_Click(object sender, System.EventArgs e) { webBrowser1.Stop(); } private void refreshButton_Click(object sender, System.EventArgs e) { webBrowser1.Refresh(); } private void Halogen_Load(object sender, System.EventArgs e) { webBrowser1.GoHome(); } private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { } } } The line that keeps giving me the error is the one with the qoutations. Note that the qoutations are not in the original code. I only get the error when I compile the program and try to use the go button to go to a web site. This is the error - Object reference not set to an instance of an object.Any tips would be greatly appreciated. Thanks Sincerely 007

##### Share on other sites
Enselic    829
Is it comboBox1 or webBrowser1 which is null?

(Tip, enclose your source with [source][/source] tag-pairs when posting code)

##### Share on other sites
agent 007    100
I'm not sure, I think it's comboBox1.

##### Share on other sites
Nik02    4349
The combo box's selected item can also be null, if the said combo box doesn't have any items yet.

##### Share on other sites
Enselic    829
Either use a debugger or write something like
    if (comboBox == null) {        MessageBox.Show("comboBox is null!");    }

to find out which it is.

##### Share on other sites
Nik02    4349
In addition, you could just use combo box's Text property to determine what is selected. It will return an empty string if the box doesn't have any items or if no item is selected.

EDIT: I recommend learning how to use QuickWatch (or ordinary Watch), it is perfect when you want to know your objects' states during a debug break.

##### Share on other sites
u235    359
Will spelling something wrong cause that error? The reason I ask is that in the problem line:

webBrowser1.Navigate(new Uri(comboBox1.SelectedItem.ToString()));

Shouldn't it be Url and not Uri? Just a thought.

-AJ

##### Share on other sites
Arild Fines    968
It's the .SelectedItem. Since he doesn't have the declarations and instantiations of the ComboBox and the WebBrowser, it's reasonable to assume those are in the designer code, which always instantiates controls.

##### Share on other sites
agent 007    100
Hey!

Thanks for all of the quick replys, but I'm still not sure what I should do...

##### Share on other sites
Nik02    4349
As I advised, use the combo box's Text property instead of it's SelectedItem one. If the combo box is initialized (and in your case, it very likely is), it's Text property always exists even though it would contain an empty string. This in contrast to SelectedItem which may not even exist because either a: you haven't got any items in the box yet or b: none of the existing items are actually selected.

##### Share on other sites
agent 007    100
Hey! Thanks for the help, but now when I try the text idea, I get a new error with the same line. "Invalid URI: The format of the URI could not be determined." And it says at the top "UriFormatException was unhandled."

##### Share on other sites
Nik02    4349
The Uri class documentation explains what constitutes a "proper" uniform resource identifier.