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
Is it comboBox1 or webBrowser1 which is null?
(Tip, enclose your source with [source][/source] tag-pairs when posting code)
(Tip, enclose your source with [source][/source] tag-pairs when posting code)
The combo box's selected item can also be null, if the said combo box doesn't have any items yet.
Either use a debugger or write something like
to find out which it is.
if (comboBox == null) { MessageBox.Show("comboBox is null!"); }
to find out which it is.
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.
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.
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
webBrowser1.Navigate(new Uri(comboBox1.SelectedItem.ToString()));
Shouldn't it be Url and not Uri? Just a thought.
-AJ
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement