Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is "Object reference not set to an instance of an object" 1

Status
Not open for further replies.

salimwng

IS-IT--Management
Mar 11, 2002
134
MU
Hi all,

I setup a form that allows the user to change the password. This password is stored in a Database (MSAccess) and the table name is Admin. There's only one field in it, named Passwd.

When i run the program, i constantly get above error message "Object reference not set to an instance of an object.". There's a prompt that appears, where i have to select Break or Continue. When i click on the Continue, the program stops at a specific line. Please see below the coding. The error is at the line showing "dbConn.Open()"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim bol As Boolean
Dim dr As DataRow

If TextBox1.Text = "" Then
MsgBox("Old Password Must Be Filled !", MsgBoxStyle.Critical, "Sales System")
TextBox1.Focus()
Exit Sub
End If

If TextBox2.Text = "" Then
MsgBox("New Password Must Be Filled !", MsgBoxStyle.Critical, "Sales System")
TextBox2.Focus()
Exit Sub
End If

PasswordKeyin = TextBox1.Text

bol = False

If PasswordKeyin <> "" Then
For Each dr In PasswdUtil_DataSet.Tables(0).Rows
If dr("Passwd") = PasswordKeyin Then
bol = True
End If
Next dr
End If

If bol = True Then
dbConn.Open() '<<----- Error is here
PasswdUtil_SQLCommand = "UPDATE Admin SET Passwd= '" & TextBox2.Text & "' WHERE Passwd = '" & TextBox1.Text & "' "
Dim cmd As New OleDbCommand(PasswdUtil_SQLCommand, dbConn)
cmd.ExecuteNonQuery()
MsgBox("Password Saved Successfully !", MsgBoxStyle.Information, "Sales System")
dbConn.Close()
Me.Close()
Else
MsgBox("Your Old Password Is Wrong !", MsgBoxStyle.Critical, "Sales System")
TextBox1.Clear()
TextBox2.Clear()
TextBox1.Focus()
End If

End Sub

=====

The Form name is PasswdUtil_Form, and i have this loading event as well.

Private Sub PasswdUtil_Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Make The Data Adapter
PasswdUtil_DataAdapter = New OleDbDataAdapter(PasswdUtil_SQLCommand, ConStr)

'Map The default Name Table
PasswdUtil_DataAdapter.TableMappings.Add("Table", "Admin")

'Clear then Fill the Data Set
PasswdUtil_DataSet.Clear()
Try
PasswdUtil_DataAdapter.Fill(PasswdUtil_DataSet)
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub

=============

In my Module section , i have these;

Public DBPath As String = System.Configuration.ConfigurationSettings.AppSettings("DbLocation")
Public ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath
Public dbConn As OleDbConnection

=======

Could any body advise me why i constantly get this error message ?

Thank you
Salim

 
Somewhere in your code, before you call dbConn.Open(), you need to set dbConn to an actual object reference, as you do with your DataAdapter, PasswdUtil_DataAdapter:

dbConn = New OleDbConnection(ConStr)



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
In general "Object reference not set to an Instance of an Object means the following:

Reference type variables store the address of an object (a 32 bit number that represents the actual memory location where the start of the object is stored) When you create a variable for a reference type without using the New keyword you merely create a variable capable of holding an address (32 bit integer) but the variable does not yet contain an address (it contains 0 I believe) which can be tested with
if Variable Is Nothing then
'the variable points to nothing
else
'the variable contains an address
end if
This is called a null pointer in other languages. However if you create the variable using the New keyword either when the variable was declared as in :

Private VariableName as New ClassName

or in 2 steps as in:

Private VariableName as ClassName
VariableName = new TypeName

the the object is created somewhere else (in the free store to give it a name (or in the heap if you prefer)) and the address of the object is assigned to the reference variable that you declared. Once you've done that then you can call the properties and methods of the object using the refernce variable.

On the other hand, if you try to call the methods or proerties of the object before creating the object (with the New keyword) than you have a null pointer (Nothing) which will give you the wonderfull "Object Reference Not Set.." error which becomes like an old friend.

In your code, it seems that dbConn is must be an object of type OleDBConnection and since it is not declared in your procedure, it must be declared somewhere else. Seems that you must instantiate the object so that reference variable holds the address of acuatl object not just Nothing. You do this with dbConn = new OleDBConnection(PutYourConnectionStirngHere). See the help file for the constructor for OleDBConnection.

-Ken






Private
 
That's a pretty good write up Ken, you should stick that in a FAQ.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Hi KenFalk,

Many thanks for this advise. My program now runs great.

Thank you once again, and to you too Jebenson.

You deserve a star.
Regards.
Salim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top