C# Error

Started by
10 comments, last by Nik02 18 years, 1 month ago
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
Advertisement
Is it comboBox1 or webBrowser1 which is null?

(Tip, enclose your source with [source][/source] tag-pairs when posting code)
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
I'm not sure, I think it's comboBox1.
The combo box's selected item can also be null, if the said combo box doesn't have any items yet.

Niko Suni

Either use a debugger or write something like
    if (comboBox == null) {        MessageBox.Show("comboBox is null!");    }

to find out which it is.
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
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.

Niko Suni

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
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
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.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Hey!


Thanks for all of the quick replys, but I'm still not sure what I should do...
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.

Niko Suni

This topic is closed to new replies.

Advertisement