Sign in to follow this  
agent 007

C# Error

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this