• Advertisement
Sign in to follow this  

C# Error

This topic is 4367 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
Advertisement
Is it comboBox1 or webBrowser1 which is null?

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

Share this post


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

Share this post


Link to post
Share on other sites
Either use a debugger or write something like

if (comboBox == null) {
MessageBox.Show("comboBox is null!");
}

to find out which it is.

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
The Uri class documentation explains what constitutes a "proper" uniform resource identifier.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement