runtime error using new [modified] ....resolved
-
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
-
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
you never set the bounds on
item()
. you need aReDim Preserve item(drcounter)
...or you could just do
Redim item(dt.Rows.Count() - 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
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.
-
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.
resolved by William Winner
-
you never set the bounds on
item()
. you need aReDim Preserve item(drcounter)
...or you could just do
Redim item(dt.Rows.Count() - 1)
Thanks, It worked. I used
Redim item(dt.Rows.Count() - 1)
to resolve issue after reading the data table.
-
Thanks, It worked. I used
Redim item(dt.Rows.Count() - 1)
to resolve issue after reading the data table.
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.