Results 1 to 7 of 7

Thread: Visual Basic - System.NullReferenceException!

  1. #1
    Join Date
    Aug 2006

    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)
    End Sub

    Does someone know how to remove this NullReferenceException ?? Please help as soon as possible.!!
    Last edited by Soumen; 16-01-2010 at 03:36 PM.
    QTechnology Ultra-Quiet ATX PSU 460W I MSI K8N Neo4-F I AMD Opteron 144 CABNE0545 2.66Ghz I 2 x 512MB RAM I 380GB Maxtor SATAI Raid0 Hard Drive I 40GB Maxtor IDE Hard Drive I Nvidia GeForce 7900GTX I Win XP Home Service Pack 2 I Soundblaster Xtreme Fidelity Fatal1ty I Mercury Home Theater HT-4500

  2. #2
    Join Date
    Feb 2008

    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.

  3. #3
    Join Date
    Oct 2005

    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..!!

  4. #4
    Join Date
    May 2008

    Re: Visual Basic - System.NullReferenceException!

    A NullReferenceException is thrown in a few instances, in most Object Oriented Programming Languages :
    1. Attempt to assign a value of null to a non-nullable type.
    2. Attempt to use a variable with a null value in an invalid context.
    3. 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.

  5. #5
    Join Date
    Nov 2005

    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 :
    TrialType combobox1;
    After doing this try to replace them with a blank assignment. For example :
    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.

  6. #6
    Join Date
    May 2008

    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.

  7. #7
    Join Date
    Apr 2008

    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.
    [ tryStatements ]
    [ Exit Try ]
    [ Catch [ exception [ As type ] ] [ When expression ]
    [ catchStatements ]
    [ Exit Try ] ]
    [ Catch ... ]
    [ Finally
    [ finallyStatements ] ]
    End Try
    Hope using this will help you.!!

Similar Threads

  1. Query Regarding Visual Basic.Net
    By Renderman21 in forum Software Development
    Replies: 4
    Last Post: 20-03-2009, 03:49 PM
  2. what are Visual Basic IDE?
    By Naresh Modi in forum Software Development
    Replies: 2
    Last Post: 06-03-2009, 09:49 AM
  3. Is GUI same like Visual Basic ?
    By Caesar in forum Software Development
    Replies: 2
    Last Post: 02-03-2009, 01:32 PM
  4. Visual Basic 2005 or Visual Basic 6
    By Aasha in forum Software Development
    Replies: 5
    Last Post: 15-01-2009, 06:56 PM
  5. Visual Basic on LAN
    By djbbenn in forum Software Development
    Replies: 2
    Last Post: 05-08-2008, 03:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts