Visual Basic - System.NullReferenceException!
I have written the code in Visual Basic, in which I am getting the 'System.NullReferenceException' in the line CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ComboBox1.Text). I am giving my code below :
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
Does someone know how to remove this NullReferenceException ?? Please help as soon as possible.!!
Re: Visual Basic - System.NullReferenceException!
The System.NullReferenceException occurs when you are addressing something that doesn't exist. That's why it pop ups the exception. In other words we can say that a when you try to reference an object in your code that does not exist then there occurs a NullReferenceException. For example, you may have tried to use an object without using the New keyword first, or tried to use an object whose value is set to null. The exception that is thrown when there is an attempt to dereference a null object reference is known as the NullReferenceException Class. Here is the syntax for that :
Visual Basic (Declaration)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class NullReferenceException _
Inherits SystemException
NullReferenceException uses the HRESULT COR_E_NULLREFERENCE, which has the value 0x80004003.
Re: Visual Basic - System.NullReferenceException!
Yes, you get the System.NullReferenceException when you are addressing some source that doesn't exist. As you mentioned that you are getting the error at CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ComboBox1.Text). So check whether the ComboBox1 and the tabcontrol1 both exists. It will show you if they contain a value, if you set a breakpoint, and then hover your mouse over those items. Doing this will let you know that the where the problem is residing. Hope you got the point that you were searching..!!
Re: Visual Basic - System.NullReferenceException!
A NullReferenceException is thrown in a few instances, in most Object Oriented Programming Languages :
- Attempt to assign a value of null to a non-nullable type.
- Attempt to use a variable with a null value in an invalid context.
- Attempt to reference a variable which you have declared, but to which no value has been assigned.
For answering your question, i will have to explain what are the 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 be sure that these variables are not just declared, but have ASSIGNED to use them. Also a null or unassigned value is not valid in most contexts. I would also like to suggest you to check your assignment statements and make sure they just use the "=" operator.
Re: Visual Basic - System.NullReferenceException!
You can try to debug your program. For the debugging purposes, if you have a blank declaration statement for these variables in your code, you can use this example :
CODE
TrialType combobox1;
After doing this try to replace them with a blank assignment. For example :
CODE
TrialType combobox1 = new TrialType();
If this code fixes the NullReferenceException, then your problem is residing within the variable that has not been assigned to at the time when you try to reference its value.
Re: Visual Basic - System.NullReferenceException!
I would like you to suggest to use the New keyword to create the instance. Have you tried to use an object without providing an instance of the object ?? For example you can use the Dim CustomerTable As DataTable should be rewritten as Dim CustomerTable As New DataTable. Also have you tried to use Explicitly catch NullReferenceException in a Try…Catch…Finally statement. According to me a Try…Catch…Finally statement can check for specific types of exceptions, going from most specific to least specific.
Re: Visual Basic - System.NullReferenceException!
Even i think that using a Try...Catch...Finally Statement can resolve your problem. A Try...Catch...Finally Statement provides a way to handle some or all possible errors that may occur in a given block of code, while still running code.
Try
[ tryStatements ]
[ Exit Try ]
[ Catch [ exception [ As type ] ] [ When expression ]
[ catchStatements ]
[ Exit Try ] ]
[ Catch ... ]
[ Finally
[ finallyStatements ] ]
End Try
Hope using this will help you.!!