Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. runtime error using new [modified] ....resolved

runtime error using new [modified] ....resolved

Scheduled Pinned Locked Moved Visual Basic
debugginghelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Daniel Engelkes
    wrote on last edited by
    #1

    each time I run the follow code I get a runtime error.

    Public Class menuitems
        Public menuitem As String
        Public itemcost As Double
    
    End Class
    'Dim totalorderquantity(12) as Integer
    Public Class frmtodayscafe
        Inherits System.Windows.Forms.Form
    
        Public dr As DataRow
        Public item() As menuitems
    
    
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim fileNamex As String = "menu1.txt"
            Dim dirName As String = _
                        Path.GetDirectoryName(Application.ExecutablePath)
            Dim dt As DataTable
            Dim adapter1 As New OleDbDataAdapter
    
            Using cn As New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;" & _
                        "Data Source=" & dirName & ";" & _
                        "Extended Properties=""Text;HDR=Yes;FMT=Delimited""")
                ' Open the connection
                cn.Open()
    
                ' Set up the adapter
                Using adapter As New OleDbDataAdapter( _
                        "SELECT * FROM " & fileNamex, cn)
                    dt = New DataTable("menuitems")
                    adapter.Fill(dt)
    
                End Using
    
            End Using
            
            Redim item(dt.Rows.Count() - 1)
    
            Dim drcounter As Integer = 0
            For Each  dr As datarow In dt.Rows
                Debug.Print("{0}    {1}", _
                            dr("Menu Items"), _
                            dr("Item Cost"))
                item(drcounter).menuitem = dr("Menu Items").ToString
                item(drcounter).itemcost = dr("Item Cost").ToString
                drcounter += 1
            Next
    

    the run time error is Object reference not set to an instance of an object.

    modified on Friday, March 19, 2010 6:50 PM

    W L 2 Replies Last reply
    0
    • D Daniel Engelkes

      each time I run the follow code I get a runtime error.

      Public Class menuitems
          Public menuitem As String
          Public itemcost As Double
      
      End Class
      'Dim totalorderquantity(12) as Integer
      Public Class frmtodayscafe
          Inherits System.Windows.Forms.Form
      
          Public dr As DataRow
          Public item() As menuitems
      
      
      
      
          Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      
              Dim fileNamex As String = "menu1.txt"
              Dim dirName As String = _
                          Path.GetDirectoryName(Application.ExecutablePath)
              Dim dt As DataTable
              Dim adapter1 As New OleDbDataAdapter
      
              Using cn As New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;" & _
                          "Data Source=" & dirName & ";" & _
                          "Extended Properties=""Text;HDR=Yes;FMT=Delimited""")
                  ' Open the connection
                  cn.Open()
      
                  ' Set up the adapter
                  Using adapter As New OleDbDataAdapter( _
                          "SELECT * FROM " & fileNamex, cn)
                      dt = New DataTable("menuitems")
                      adapter.Fill(dt)
      
                  End Using
      
              End Using
              
              Redim item(dt.Rows.Count() - 1)
      
              Dim drcounter As Integer = 0
              For Each  dr As datarow In dt.Rows
                  Debug.Print("{0}    {1}", _
                              dr("Menu Items"), _
                              dr("Item Cost"))
                  item(drcounter).menuitem = dr("Menu Items").ToString
                  item(drcounter).itemcost = dr("Item Cost").ToString
                  drcounter += 1
              Next
      

      the run time error is Object reference not set to an instance of an object.

      modified on Friday, March 19, 2010 6:50 PM

      W Offline
      W Offline
      William Winner
      wrote on last edited by
      #2

      you never set the bounds on item(). you need a

      ReDim Preserve item(drcounter)

      ...or you could just do

      Redim item(dt.Rows.Count() - 1)

      D 1 Reply Last reply
      0
      • D Daniel Engelkes

        each time I run the follow code I get a runtime error.

        Public Class menuitems
            Public menuitem As String
            Public itemcost As Double
        
        End Class
        'Dim totalorderquantity(12) as Integer
        Public Class frmtodayscafe
            Inherits System.Windows.Forms.Form
        
            Public dr As DataRow
            Public item() As menuitems
        
        
        
        
            Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
                Dim fileNamex As String = "menu1.txt"
                Dim dirName As String = _
                            Path.GetDirectoryName(Application.ExecutablePath)
                Dim dt As DataTable
                Dim adapter1 As New OleDbDataAdapter
        
                Using cn As New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;" & _
                            "Data Source=" & dirName & ";" & _
                            "Extended Properties=""Text;HDR=Yes;FMT=Delimited""")
                    ' Open the connection
                    cn.Open()
        
                    ' Set up the adapter
                    Using adapter As New OleDbDataAdapter( _
                            "SELECT * FROM " & fileNamex, cn)
                        dt = New DataTable("menuitems")
                        adapter.Fill(dt)
        
                    End Using
        
                End Using
                
                Redim item(dt.Rows.Count() - 1)
        
                Dim drcounter As Integer = 0
                For Each  dr As datarow In dt.Rows
                    Debug.Print("{0}    {1}", _
                                dr("Menu Items"), _
                                dr("Item Cost"))
                    item(drcounter).menuitem = dr("Menu Items").ToString
                    item(drcounter).itemcost = dr("Item Cost").ToString
                    drcounter += 1
                Next
        

        the run time error is Object reference not set to an instance of an object.

        modified on Friday, March 19, 2010 6:50 PM

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        compile-time errors and run-time exceptions tend to provide line numbers (assuming a debug build), so you should be able to easily find where the problem occurs, and then deduce where it originated. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


        D 1 Reply Last reply
        0
        • L Luc Pattyn

          compile-time errors and run-time exceptions tend to provide line numbers (assuming a debug build), so you should be able to easily find where the problem occurs, and then deduce where it originated. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


          D Offline
          D Offline
          Daniel Engelkes
          wrote on last edited by
          #4

          resolved by William Winner

          1 Reply Last reply
          0
          • W William Winner

            you never set the bounds on item(). you need a

            ReDim Preserve item(drcounter)

            ...or you could just do

            Redim item(dt.Rows.Count() - 1)

            D Offline
            D Offline
            Daniel Engelkes
            wrote on last edited by
            #5

            Thanks, It worked. I used

            Redim item(dt.Rows.Count() - 1)
            

            to resolve issue after reading the data table.

            W 1 Reply Last reply
            0
            • D Daniel Engelkes

              Thanks, It worked. I used

              Redim item(dt.Rows.Count() - 1)
              

              to resolve issue after reading the data table.

              W Offline
              W Offline
              William Winner
              wrote on last edited by
              #6

              glad it worked. Most of the time, we just need a separate set of eyes on our code. I can't tell you how many times I've gotten errors because I forgot a simple line of code.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups