Click here to Skip to main content
15,892,281 members

Response to: object reference not set to an instance of an object VB.NET

Revision 3
First thing you need to check is that all the data tables passed in your method should not be initialized rather than Nothing.

For example, the start of method will be something like this: (In this case following line will not break:

For Each total As DataRow In Total_PerItem.Rows)

VB
Public Sub Plot_TotalPerItem(ByRef NewTable As DataTable, ByVal Items As DataTable, ByVal Total_PerItem As DataTable)

If(NewTable Is Nothing OR NewTable Is Nothing OR NewTable Is Nothing)
   'Show Error Message
  return
End if

Try
            'THE ERROR IS HERE
 For Each total As DataRow In Total_PerItem.Rows
..
..
..


Second thing is this part:

VB
If total.Item("ItemCode") = item.Item("ItemCode") And total.Item("ItemClass") = item.Item("ItemClass") Then


If you want to take decision on the basis of two conditions then prefer AndAlso rather than And. AndAlso helps to evaluate one condition at a time Read it from here.

Then check for NULL values

Finally, check any missing column name:

VB
item.Item("ItemCode")  ' ItemCode must be in DataTable


I hope it can help you to figure out problem.
Posted 11-Oct-12 0:20am by Shahan Ayyub.
Tags:
  Print Answers RSS
Top Experts
Last 24hrsThis month