473,396 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

System.NullReferenceException: Object reference not set to an instance of an object

1
i'm getting this error whenever i run this page.
My codes:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Dim con As String = "Database=barcouncil;Data Source=dev;UserId=root;password=;"
  3.         Dim cn As New MySqlConnection
  4.         Dim da As New MySqlDataAdapter
  5.  
  6.         'da.Fill(ds, "firm_headquarters")
  7.         Try
  8.             cn.Open()
  9.             da.SelectCommand.ExecuteNonQuery()
  10.             da.Fill(ds, "firm_headquarters")
  11.  
  12.  
  13.  
  14.             DT = ds.Tables("firm_headquarters")
  15.             Dim dr As DataRow
  16.             dr = DT.NewRow
  17.  
  18.             dr(2) = TextBox2.Text
  19.             dr(4) = TextBox3.Text
  20.             dr(6) = TextBox4.Text
  21.             dr(8) = TextBox5.Text
  22.             dr(10) = TextBox6.Text
  23.             dr(3) = TextBox7.Text
  24.             dr(11) = LinkButton2.Text
  25.  
  26.             dr.EndEdit()
  27.             Response.Write("Updated")
  28.             DT.Rows.Add(dr)
  29.  
  30.             Dim cb As New MySqlCommandBuilder(da)
  31.             da.Update(ds, "firm_headquarters")
  32.  
  33.         Catch ex As Exception
  34.             Response.Write(ex.ToString)
  35.         End Try
  36.  
  37.  
  38.     End Sub
can you guys help me here?
tq
Dec 4 '09 #1
3 3034
MrMancunian
569 Expert 512MB
On what line did you get this error? In general, this kind of error occurs when you didn't declare the object you're handling as New.

Steven
Dec 4 '09 #2
For one thing, I see you are using a variable named ds in da.Fill(ds, "firm_headquarters"), but I don't see it anywhere before it. Did you meant to use cn instead of ds, or is ds a global variable in another file in your project?

Also, I see that you declared the function cn but I don't see you using the con string. Are you sure you are not asked for parameters by cn when you used the new keyword like Dim cn As New MySqlConnection = something, or Dim cn As New MySqlConnection(something)? This is something I have had trouble with in the past when I have declared objects and did not know they had parameters that had to be especified.
Dec 6 '09 #3
Frinavale
9,735 Expert Mod 8TB
A NullReferenceException occurs when you try to use something that hasn't been instantiated or doesn't exist (is null).

For example you would get a NullReferenceException if you did something like:
Expand|Select|Wrap|Line Numbers
  1. Dim myPerson As Person
  2. myPerson .FirstName="Frinny"
  3.  
The reason is because myPerson has not been instantiated as a New Person. This means that memory for myPerson has not been allocated and that myPerson points to "Null"/"Nothing" (no memory location).

So if you try access any of myPerson's properties or methods you'll get a NullReferenceException because these properties/methods are not available since myPerson is pointing to Null/Nothing/No-memory.

To fix the NullReferenceException, all I have to do is use the keyword 'new'. This will instantiate the object. Instantiating the object will allocate memory (and execute the constructor) for the object so that you can use it:

For example, the following will instantiate a Person Object:
Expand|Select|Wrap|Line Numbers
  1. Dim myPerson As New Person
  2.  
So will:
Expand|Select|Wrap|Line Numbers
  1. Dim myPerson As Person
  2. myPerson = New Person
  3.  
Sometimes you will call methods that should return you an instance of an Object.

For example, say there was a method called "RetrievePerson()" that should return a person object, or if it couldn't it should return nothing. Before you attempt to use any properties or methods for any object returned by a method you should always check to make sure that it exists first. To do this you just check if the object is "nothing".

For example:
Expand|Select|Wrap|Line Numbers
  1. Dim myPerson As Person = RetrievePerson()
  2. If myPerson is Nothing Then
  3.   myPerson = new Person 'This line allocates memory for the Person Object
  4. End If
  5. myPerson.FirstName = "Frinny"
  6. ....
  7.  
In your case you never checked to see if "ds" exists.
You also never checked to see if "DT" exists.....

You are going to get this error as soon as you try to use an object that has not been instantiated.

-Frinny
Dec 7 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
7
by: mike p. | last post by:
I have a docbook xml file, and am using standard docbook 1.61.3 xsl stylesheets to do xhtml transform. Transform works fine when using MSXML. When I try to do the following using asp.net 1.1: ...
0
by: muralidharan | last post by:
WebForm1.aspx Code: <%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView id="TreeView1" Height="520"...
5
by: Vitling | last post by:
For no apparent reason, a NullReference exception is thrown in system.dll (System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback). Since I only get a disassembly from Visual Studio, it...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
1
by: msnews.microsoft.com | last post by:
I'm trying to fill an array of objects but when I add the first object I get a NullReferenceException. ----------------------------------------------------------------------------...
4
by: Terry Mulvany | last post by:
I have a 'BasePage' (BasePage.cs) derived from System.Web.UI.Page that all my pages inherit from. I need to set some properties (either in the OnInit or constructor) based on a potential...
2
by: sxiao | last post by:
Hi, there I got a NullReferenceException when there are more than one users trying to open the same page at the same time. The senerio is: Two users logged into the web application using the...
3
by: Brano | last post by:
HI all, I have a problem i have a web application that was working fine and this morning when i run it and click on a button that does Reponse.Redirect to a page i get this error : Server...
6
by: William Mild | last post by:
I must be getting brain fried. I can't see the error. Create a new web form with the following code begind: Public Class test Inherits System.Web.UI.Page Public Class ReportCardData ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.