Please Help to fix this Error "object reference not set to instance of an object"

delion

Member
Joined
Apr 27, 2007
Messages
7
Programming Experience
Beginner
Please Help to fix this Error "object reference not set to instance of an object"

Help Me Please
Why this Message Box appear with this message "object reference not set to instance of an object"
When I type this code


VB.NET:
Imports System.Data
Imports System.Data.OleDb
Imports System.Text.RegularExpressions
Public Class frmtoken
Public Enum Data As Integer
id_Mail = 0
sbjMail = 1
End Enum
Private Sub btnTkn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTkn.Click
Dim conDtbsSkrip As OleDbConnection = New OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=E:\dtbsSkrip.mdb")
Dim cmdDtbsSkrip As New OleDbCommand()
Dim objDataAdapter As New OleDbDataAdapter
Dim objDataTable As New DataTable
Dim r As New Regex("[^a-zA-Z]")
Try
conDtbsSkrip.Open()
cmdDtbsSkrip.Connection = conDtbsSkrip
'COUNT RECORD sbjMail
cmdDtbsSkrip.CommandType = CommandType.Text
cmdDtbsSkrip.CommandText = _
"SELECT COUNT (*) FROM tblMail WHERE sbjMail"
Dim returnValue As Object
returnValue = cmdDtbsSkrip.ExecuteScalar()
'GET RECORD id_Mail AND sbjMail
cmdDtbsSkrip.CommandType = CommandType.Text
cmdDtbsSkrip.CommandText = _
"SELECT * FROM tblMail"
Dim reader As OleDbDataReader = cmdDtbsSkrip.ExecuteReader()
'RECORD TO ARRAY 1
Dim Arr1(returnValue, 1) As String
Dim x1 As Integer = 0
Dim Tmp1_sbjMail As String = ""
Dim Tmp1_idMail As String = ""
If (reader.HasRows) Then
While reader.Read()
Tmp1_sbjMail = reader("sbjMail")
Tmp1_idMail = reader("id_Mail")
If r.Match(Tmp1_sbjMail).Success Then
Tmp1_sbjMail = r.Replace(Tmp1_sbjMail, " ")
End If
Arr1(x1, Data.id_Mail) = Tmp1_idMail
Arr1(x1, Data.sbjMail) = Tmp1_sbjMail.Trim.ToLower
'lst1.Items.Add("H1:" & Arr1(x1, 0) & "H3:" & Arr1(x1, 1))
x1 = x1 + 1
End While
End If
reader.Dispose()
reader = Nothing
'RECORD TO ARRAY 3
Dim Arr3() As String
Dim x3 As Integer = 0
Dim x33 As Integer = 0
Dim Tmp3_idMail As String = ""
Dim Tmp3_sbjMail As String = ""
Dim Tmp3_kata As String = ""
For x3 = 0 To x1
Tmp3_idMail = Arr1(x3, 0)
Tmp3_sbjMail = Arr1(x3, 1)
Arr3 = Tmp3_sbjMail.Split(" ") 'ERROR START FROM HERE
MsgBox(Arr3(x3))
For x33 = 0 To UBound(Arr3)
Tmp3_kata = Arr3(x33).Trim
If (Tmp3_kata <> "") Then
cmdDtbsSkrip.CommandType = CommandType.Text
cmdDtbsSkrip.CommandText = _
"INSERT INTO tblKata(id_Mail,Kata) values (@id_Mail,@Kata)"
cmdDtbsSkrip.Parameters.AddWithValue("@id_Mail", Tmp3_idMail)
cmdDtbsSkrip.Parameters.AddWithValue("@Kata", Tmp3_kata)
cmdDtbsSkrip.Parameters.AddWithValue("@Status", 1)
cmdDtbsSkrip.ExecuteNonQuery()
cmdDtbsSkrip.Parameters.Clear()
End If
Next x33
Next x3
'VIEW DATA TO FORM
cmdDtbsSkrip.CommandType = CommandType.Text
cmdDtbsSkrip.CommandText = _
"SELECT * FROM tblKata"
objDataAdapter.SelectCommand = cmdDtbsSkrip
objDataAdapter.Fill(objDataTable)
grdToken.DataSource = objDataTable
grdToken.AlternatingRowsDefaultCellStyle.BackColor = Color.WhiteSmoke
grdToken.CellBorderStyle = DataGridViewCellBorderStyle.None
grdToken.SelectionMode = DataGridViewSelectionMode.FullRowSelect
MessageBox.Show("END ....")
Catch OleDbExceptionErr As OleDbException
MessageBox.Show(OleDbExceptionErr.Message, "Access SQL1")
Catch InvalidOperationExceptionErr As InvalidOperationException
MessageBox.Show(InvalidOperationExceptionErr.Message, "Access SQL3")
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cmdDtbsSkrip.Dispose()
cmdDtbsSkrip = Nothing
conDtbsSkrip.Close()
conDtbsSkrip = Nothing
End Try
End Sub
End Class
Help me To fix it
Thanks :)
 
Last edited by a moderator:
Most of the time, this kind of error is being thrown if you refer to a non-object. Objects are created with the keyword New. VS.NET has a very good tool of pointing to that line of error if an error is thrown. But as i can see in your code in this line:

While reader.Read()
Tmp1_sbjMail = reader("sbjMail")
Tmp1_idMail = reader("id_Mail")

You didn't specify a method for the OleDbDataReader. Try this:

While reader.Read()
Tmp1_sbjMail = reader.item("sbjMail")
Tmp1_idMail = reader.item("id_Mail")

Which reader.item returns an object the result is if you didn't iclude the item method the Tmp1_sbjMail and Tmp1_idMail will refer to a non-object.

Hope this helps
 
Back
Top