Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Visual Basic - System.NullReferenceException!

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 16-01-2010
Member
 
Join Date: Aug 2006
Posts: 181
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.!!

__________________
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

Last edited by Soumen : 16-01-2010 at 02:36 PM.
Reply With Quote
  #2  
Old 16-01-2010
Member
 
Join Date: Feb 2008
Posts: 1,845
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.
Reply With Quote
  #3  
Old 16-01-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
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..!!
Reply With Quote
  #4  
Old 16-01-2010
Member
 
Join Date: May 2008
Posts: 2,381
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.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 16-01-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
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.
Reply With Quote
  #6  
Old 16-01-2010
Member
 
Join Date: May 2008
Posts: 2,290
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.
Reply With Quote
  #7  
Old 16-01-2010
Member
 
Join Date: Apr 2008
Posts: 2,000
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.!!
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Visual Basic - System.NullReferenceException!"
Thread Thread Starter Forum Replies Last Post
Query Regarding Visual Basic.Net Renderman21 Software Development 4 20-03-2009 02:49 PM
what are Visual Basic IDE? Naresh Modi Software Development 2 06-03-2009 08:49 AM
Is GUI same like Visual Basic ? Caesar Software Development 2 02-03-2009 12:32 PM
Visual Basic 2005 or Visual Basic 6 Aasha Software Development 5 15-01-2009 05:56 PM
Visual Basic on LAN djbbenn Software Development 2 05-08-2008 02:15 PM


All times are GMT +5.5. The time now is 06:03 AM.