Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Visual basic System.NullReferenceException


  • Please log in to reply

#1
Mr White

Mr White

    Member

  • Member
  • PipPip
  • 53 posts
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!
  • 0

Advertisements


#2
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
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.
  • 0

#3
Mr White

Mr White

    Member

  • Topic Starter
  • Member
  • PipPip
  • 53 posts
Can you explain me how to do that because i am a noob in VB i am not a genius i am learning now :) it would be very good if you could tell me how to do it and yes combobox1 and tab control1 does exist this null reference come up when i added a history sistem in my browser. :)
  • 0

#4
W-Unit

W-Unit

    Member

  • Member
  • PipPipPip
  • 170 posts
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.
SomeType combobox1;
...try replacing them with a blank assignment, i.e.
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.

Edited by W-Unit, 02 September 2009 - 08:43 PM.

  • 0

#5
Mr White

Mr White

    Member

  • Topic Starter
  • Member
  • PipPip
  • 53 posts
This is the full source 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)

Edited by Mr White, 03 September 2009 - 01:29 AM.

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP