Results 1 to 6 of 6

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

  1. #1
    delion is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2007
    Posts
    7
    Reputation
    0

    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


    Code:
     
    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 JohnH; 05-13-2007 at 8:17 AM. Reason: code box

  2. #2
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,027
    Reputation
    1706
    Um.. please read http://www.vbdotnetforums.com/misc.php?do=bbcode - the section about [CODE] tags.
    Also googling for "object reference not set" will give you hundreds of hits; its one of the most basic errors there is

  3. #3
    the_atom is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2007
    Posts
    26
    Reputation
    78
    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

  4. #4
    delion is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2007
    Posts
    7
    Reputation
    0
    It still happen

    this line
    Arr3 = Tmp3_sbjMail.Split(" ") 'ERROR START FROM HERE

  5. #5
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,335
    Reputation
    960
    what's Arr3?
    what's Tmp3_sbjMail?
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  6. #6
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,027
    Reputation
    1706
    Quote Originally Posted by JuggaloBrotha View Post
    what's Arr3?
    Shouldnt matter
    what's Tmp3_sbjMail?
    it Is probably Nothing

Bookmarks

Posting Permissions

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