Visual basic System.NullReferenceException, In a code i badly need x(. |
Visual basic System.NullReferenceException, In a code i badly need x(. |
Sep 2 2009, 12:43 AM
Post
#1
|
|
![]() Geek in Training ![]() ![]() Posts: 48 OS: Windows XP |
CODE Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress e.KeyChar = vbCr CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ComboBox1.Text) My.Settings.History.Add(ComboBox1.Text) ComboBox1.Items.Add(ToolStripComboBox2.Text) End Sub This is the code that i have a System.NullReferenceException this line to be more specific CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ComboBox1.Text).At Microsoft's site they said for this error to disable vb optimization which they don't tell you how So if someone know how to disable it or to resolve this NullReferenceException please tell me! |
|
|
![]() |
Sep 2 2009, 01:06 PM
Post
#2
|
|
|
Trusted Tech Posts: 2,413 OS: Vista Ultimate SP2 64-bit, XP Pro SP3 |
A null reference exception means you are addressing something that doesn't exist... Are you sure that TabControl1.SelectedTab.Controls.Item(0) of tabcontrol1 and ComboBox1.Text both exist? A good way to see is to set a breakpoint, and then hover your mouse over those items... It will show you if they contain a value.
|
|
|
Sep 2 2009, 02:06 PM
Post
#3
|
|
![]() Geek in Training ![]() ![]() Posts: 48 OS: Windows XP |
Can you explain me how to do that because i am a noob in VB i am not a genius i am learning now
|
|
|
Sep 2 2009, 08:41 PM
Post
#4
|
|
|
Member ![]() ![]() ![]() Posts: 123 OS: WinXP Media Center |
Full code would be helpful.
I don't know VB but I do know enough about OOP to understand your problem. In most OOPLs, a NullReferenceException is thrown in a few instances: 1. Attempt to reference a variable which you have declared, but to which no value has been assigned 2. Attempt to assign a value of null to a non-nullable type 3. Attempt to use a variable with a null value in an invalid context Judging from your description, I'd rule out number 2. So, to answer your question, what are combobox1 and control1's types? Are these types nullable? If not, have you attempted to assign a value of null to either of them? Also, make sure that these variables have been ASSIGNED, not just declared, before you try to use them. A null or unassigned value is not valid in most contexts. Check your assignment statements and make sure they just use the "=" operator. Operators such as +=, -=, etc, are a bit deceptive in that they ARE NOT assignment operators. You also cannot use increment or decrement operators such as ++ on unassigned variables. For debugging purposes, if you have a blank declaration statement for these variables in your code, i.e. CODE SomeType combobox1; ...try replacing them with a blank assignment, i.e. CODE SomeType combobox1 = new SomeType(); (not sure if that is how the construction operator works in VB but I think you get my point) If that fixes the NullReferenceException, then your problem is that you are somehow winding up with a variable that has not been assigned to at the time when you try to reference its value (you'll probably wind up with a different exception in this case, but that's ok - you'll have found the problem. Assuming the exception isn't about the assignment itself, of course). Otherwise please post complete code and the location of the exception in order for me to better assist. This post has been edited by W-Unit: Sep 2 2009, 08:43 PM |
|
|
Sep 3 2009, 01:23 AM
Post
#5
|
|
![]() Geek in Training ![]() ![]() Posts: 48 OS: Windows XP |
This is the full source code
CODE Imports System.Windows.Forms.VisualStyles.VisualStyleElement.Menu Public Class Form1 Dim int As Integer = 0 Private Sub Loading(ByVal sender As Object, ByVal e As Windows.Forms.WebBrowserProgressChangedEventArgs) ToolStripProgressBar1.Maximum = e.MaximumProgress ToolStripProgressBar1.Value = e.CurrentProgress End Sub Private Sub Done(ByVal sender As Object, ByVal e As Windows.Forms.WebBrowserDocumentCompletedEventArgs) TabControl1.SelectedTab.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle ComboBox1.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Url.ToString End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoBack() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoForward() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Refresh() End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Stop() End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoHome() ToolStripStatusLabel1.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).StatusText End Sub Private Sub NewTabToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewTabToolStripMenuItem.Click Dim Browser As New WebBrowser TabControl1.TabPages.Add("New Page") TabControl1.SelectTab(int) Browser.Name = "FireBird" Browser.Dock = DockStyle.Fill TabControl1.SelectedTab.Controls.Add(Browser) AddHandler Browser.ProgressChanged, AddressOf Loading AddHandler Browser.DocumentCompleted, AddressOf Done int = int + 1 End Sub Private Sub RemoveTabToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveTabToolStripMenuItem.Click If Not TabControl1.TabPages.Count = 1 Then End If TabControl1.TabPages.RemoveAt(TabControl1.SelectedIndex) TabControl1.SelectTab(TabControl1.TabPages.Count - 1) int = int - 1 End Sub Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress e.KeyChar = vbCr CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ComboBox1.Text) My.Settings.History.Add(ComboBox1.Text) ComboBox1.Items.Add(ToolStripComboBox2.Text) End Sub Private Sub RegularToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RegularToolStripMenuItem.Click CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoBack() End Sub Private Sub BlackAndWhiteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BlackAndWhiteToolStripMenuItem.Click CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoForward() End Sub Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Toolstripmenuitem.Click CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoHome() End Sub Private Sub Form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each ITEM As String In My.Settings.History ToolStripComboBox2.Items.Add(ITEM) Next End Sub Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork For Each ITEM As String In My.Settings.History ToolStripComboBox2.Items.Add(ITEM) Next End Sub Private Sub ClearAllHistoryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearAllHistoryToolStripMenuItem.Click If ToolStripComboBox2.Text Then My.Settings.History.Clear() End If If ToolStripComboBox2.Text Then ToolStripComboBox2.Items.Clear() End If End Sub End Class As for your options I chose 2.Attempt to reference a variable which you have declared, but to which no value has been assigned. This is the full error NullReferenceException was unhandled bject reference not set to an instance of an object. This appears every time when i debug my litle Browser and type a word in my combobox! This is the line where the error shows up when i debug: CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ComboBox1.Text) This post has been edited by Mr White: Sep 3 2009, 01:29 AM |
|
|
![]() ![]() |
Similar Topics
| Topic Title | Replies / Views | Topic Information | |||||
|---|---|---|---|---|---|---|---|
![]() |
4 / 448 | 9th February 2006 - 01:13 AM complete started - last by Krizniak |
|||||
![]() |
19 / 379 | 17th June 2009 - 08:15 PM atlantica started - last by atlantica |
|||||
![]() |
0 / 127 | 18th June 2009 - 10:22 PM atlantica started - last by atlantica |
|||||
![]() |
0 / 50 | 13th November 2009 - 01:52 PM -Lionel- started - last by -Lionel- |
|||||
|
Time is now: 21st November 2009 - 01:10 PM |
Advertisements do not imply our endorsement of that product or service. The forum is run by volunteers who donate their time and expertise. We make every attempt to ensure that the help and advice posted is accurate and will not cause harm to your computer. However, we do not guarantee that they are accurate and they are to be used at your own risk. All trademarks mentioned on this page are the property of their respective owners.
© Geeks to Go, Inc. | All Rights Reserved | Privacy Policy | Advertising