vb.net error in object reference not set to an instance of an object n

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 8566 Views - Last Post: 16 November 2011 - 10:12 PM Rate Topic: -----

#1 sigridish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 09-November 11

vb.net error in object reference not set to an instance of an object n

Posted 09 November 2011 - 09:48 PM

Dim iCount As Integer
        Dim iLoop As Integer
        Dim query3 As New MySqlCommand
        Dim lvitem


        ConnectDatabase()
        cona.Open()
        iCount = lvLogs.Items.Count


        If Not lvLogs.Items.Count = 0 Then
            Do Until iLoop = lvLogs.Items.Count.ToString

                lvitem = lvLogs.Items.Item(iLoop).ToString

                With lvitem
                    query3.CommandText = "insert into wer(CustomerName, SalesGroup, CustomerType, TypeOfIndustry, RM, Senior_RM) values " _
                    & "('" & .SubItems(0).Text & "','" & .SubItems(1).Text & "','" & .SubItems(2).Text & "','" & .SubItems(3).Text & "','" & .SubItems(4).Text & "','" & .SubItems(5) & "')"
                    query3.ExecuteNonQuery()
                End With

                iLoop = iLoop + 1
                lvitem = Nothing
                MsgBox("New record added!")
            Loop

        End If



i think my error is declaration of variable type.

CAN SOMEBODY HELP ME?

this code is use to save imported excel file to list view then from listview, it will be saved in the DATABASE.

Is This A Good Question/Topic? 0
  • +

Replies To: vb.net error in object reference not set to an instance of an object n

#2 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: vb.net error in object reference not set to an instance of an object n

Posted 09 November 2011 - 10:10 PM

Set a breakpoint at the beginning of the code and step through it as it executes. You'll be able to pinpoint the line that throws the exception, and you'll then be armed with much more information on how to troubleshoot your problem.

"Object reference not set to an instance of an object" doesn't happen because you've declared something to be the wrong type. They happen when some code refers to something that doesn't exist/hasn't been properly assigned to. I get the feeling your problem is that you're calling .ToString on line 15. A listviewitem is like an array -- it's a collection of objects which are accessed via a .SubItem. But when you call .ToString on line 15, you're storing a single string in lvitem, which then you're trying to access nonexistant subitems of when you build the insert statement. So, lose the .ToString and see what happens.

Also, on line 13, don't call .ToString -- never compare numbers to strings, bad things can happen.
Was This Post Helpful? 1
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: vb.net error in object reference not set to an instance of an object n

Posted 09 November 2011 - 10:15 PM

Line 15 you set lvitem to a string (because you use ToString()). But then you use a "with" statement where you try to access "SubItems" of lvitem. It is a string, it doesn't have SubItems. So when you do finally get it returning an object, make sure you are not setting lvItem to a string if you wish to get SubItems from it.


I also notice you are saying this is VB.NET yet you are using functions like MsgBox which is classic vb. You really should be using the VB.NET equivalent MessageBox.Show

:)

This post has been edited by Martyr2: 09 November 2011 - 10:19 PM

Was This Post Helpful? 1
  • +
  • -

#4 sigridish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 09-November 11

Re: vb.net error in object reference not set to an instance of an object n

Posted 09 November 2011 - 10:22 PM

thanks guys! but i still get the same message :(
Was This Post Helpful? 0
  • +
  • -

#5 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: vb.net error in object reference not set to an instance of an object n

Posted 09 November 2011 - 10:46 PM

K, go back to the first thing I said. Set a breakpoint & step through the code as it's executing. Find out what line the exception is occuring on.
Was This Post Helpful? 1
  • +
  • -

#6 sigridish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 09-November 11

Re: vb.net error in object reference not set to an instance of an object n

Posted 09 November 2011 - 10:50 PM

thanks tryparse. but how do i set a breakpoint? :(
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: vb.net error in object reference not set to an instance of an object n

Posted 09 November 2011 - 10:57 PM

If you tell us on which line and object does the error happens may be we can help.
How to debug
Was This Post Helpful? 1
  • +
  • -

#8 sigridish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 09-November 11

Re: vb.net error in object reference not set to an instance of an object n

Posted 09 November 2011 - 11:08 PM

Public Sub ConnectDatabase()

        cona = New MySqlConnection
        cona.ConnectionString = "server=localhost;User Id=root;Initial Catalog=test"
        Try
            cona.Open()

            With mycommand
                .Connection = cona
                .CommandText = "Select * from wer"
                .ExecuteNonQuery()
            End With
        Catch ex As Exception
            MessageBox.Show("Error in" & ex.Message)
        Finally
            cona.Dispose()
            cona.Close()


        End Try
    End Sub




thats where it pointed when i set the breakpoint
Was This Post Helpful? 0
  • +
  • -

#9 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: vb.net error in object reference not set to an instance of an object n

Posted 09 November 2011 - 11:11 PM

It threw the error on line 7 of the original code you posted (the line where you call ConnectDatabase())? Then it has to be throwing somewhere inside that sub. First thing I see is you should swap lines 16 & 17 -- if you dispose the connection, there's nothing left to close. Also, why is your command a query (line 10), but you're calling ExecuteNonQuery (line 11)? And why are you executing a query but never doing anything with the results of it? Also, you're trying to open cona on line 8 of the original code, but you're disposing it at the end of the sub you call right before that -- there won't be anything to open.

What exactly is it that ConnectDatabase is supposed to be doing? I looks to me like you could just replace the call to ConnectDatabase() (line 7 in the original code) with
cona = New MySqlConnection
cona.ConnectionString = "server=localhost;User Id=root;Initial Catalog=test"

and be much better off.

This post has been edited by Tryparse: 09 November 2011 - 11:22 PM

Was This Post Helpful? 1
  • +
  • -

#10 sigridish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 09-November 11

Re: vb.net error in object reference not set to an instance of an object n

Posted 09 November 2011 - 11:13 PM

thanks tryparse. i already posted the code where the error lead me. the code i recently posted was placed in my module globals
Was This Post Helpful? 0
  • +
  • -

#11 sigridish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 09-November 11

Re: vb.net error in object reference not set to an instance of an object n

Posted 09 November 2011 - 11:29 PM

what should i replace with line 10 .executenonquery?
Was This Post Helpful? 0
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: vb.net error in object reference not set to an instance of an object n

Posted 09 November 2011 - 11:41 PM

If the problem is with mycommand, then I would expect in your declaration you said Dim mycommand As MySqlCommand instead say Dim mycommand As New MySqlCommand to initialize it
Was This Post Helpful? 0
  • +
  • -

#13 sigridish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 09-November 11

Re: vb.net error in object reference not set to an instance of an object n

Posted 09 November 2011 - 11:49 PM

its still the same :(
Was This Post Helpful? 0
  • +
  • -

#14 sigridish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 09-November 11

Re: vb.net error in object reference not set to an instance of an object n

Posted 10 November 2011 - 12:15 AM

is my code for inserting data on the database using listview correct? :wacko: :sweatdrop: :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#15 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: vb.net error in object reference not set to an instance of an object n

Posted 10 November 2011 - 08:37 AM

I still don't see the point of that whole ConnectDatabase() method. I would do the following and see what happens.
Dim iCount As Integer
        Dim iLoop As Integer
        Dim query3 As New MySqlCommand
        Dim lvitem


        'ConnectDatabase()
        Dim cona As New MySqlConnection
        cona.ConnectionString = "server=localhost;User Id=root;Initial Catalog=test"
        cona.Open()
        query3.Connection = cona

        iCount = lvLogs.Items.Count


        If Not lvLogs.Items.Count = 0 Then
            Do Until iLoop = lvLogs.Items.Count.ToString

                lvitem = lvLogs.Items.Item(iLoop).ToString

                With lvitem
...

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2