Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi there! I need some help. I don't know why I'm having this error. Please help me. This is my code:
VB
Public Sub Plot_TotalPerItem(ByRef NewTable As DataTable, ByVal Items As DataTable, ByVal Total_PerItem As DataTable)
        Try
            'THE ERROR IS HERE
 For Each total As DataRow In Total_PerItem.Rows
                Dim i As Integer = 0
                For Each item As DataRow In Items.Rows
                    If total.Item("ItemCode") = item.Item("ItemCode") And total.Item("ItemClass") = item.Item("ItemClass") Then
                        NewTable.Rows(i + GetNumberOf_ColumnHeader_Rows()).Item(NewTable.Columns.Count - 1) = total.Item("Total")
                    End If
                    i = i + 1
                Next
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

HTML
Error:
"object reference not set to an instance of an object"


Thanks!!
Posted
Updated 11-Oct-12 0:36am
v4
Comments
AshishChaudha 11-Oct-12 1:47am    
Have you debugged your code..?
Janna Dela Cruz 11-Oct-12 1:49am    
yeah.
AshishChaudha 11-Oct-12 1:52am    
Please specify the line exactly where you are getting the exception.

You should always indicate the line(s) where an exception is thrown. However, this exception is one of the easiest to sort our and to fix the problem. You simply use some object of reference type by dereferencing it, assuming it is not null (Nothing in VB.NET), but in fact it appears to be null.

Most likely, this is one of the objects obtained through the indexed property as item.Item(someString), such as item.Item("ItemCode"), item.Item("ItemClass"), etc. The variable item itself should not be null, but resulting expression can be, because the item is not found by one of these string indices. And then you use this null result in calculation (for example, apply And operator), which causes an attempt to dereference the value, which will through the exception you mentioned in case of null.

The general problem is that you hard-code these strings as immediate constants, which makes it easy to misspell it. As the bug could be in the string value, the compiler cannot check up its correctness.

Generally, you need to make sure that some value used in calculations is not null, or check it for null and do not do this operation if it is null.

—SA
 
Share this answer
 
If the error really happens in the line you say, then Total_PerItem is null.
 
Share this answer
 
Comments
Member 14737637 18-Feb-20 3:45am    
Object reference not set to an instance of an object this error show using vb.net in connection command how to solve the error? so we can't insert or retrieve the data from the database table
First Check all datatables and Comparison for Nothing.
 
Share this answer
 
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:

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

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

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


In this case following line will not break:
VB
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.
 
Share this answer
 
v4
I have found the error at href so please help me

Private Sub WebBrowser1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
Dim thiselement As HtmlElement = WebBrowser1.Document.ActiveElement
Dim targeturl As String = thiselement.GetAttribute("href")
e.Cancel = True
Dim window As New Form1
window.Show()
window.WebBrowser1.Navigate(targeturl)
End Sub

at "href" i have found error like Object reference not set to an instant of object. my code is in vb.net 2010.
 
Share this answer
 
Public Function getstock() As DataTable
Dim conn As New OleDb.OleDbConnection
Dim m_lstErrors As New List(Of BuildBusiness.LDError)
Dim cmd As New OleDb.OleDbCommand
Dim da As New OleDb.OleDbDataAdapter
Dim dt As New DataTable
Try
'trnOledb = n_cnn.BeginTransaction

cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT Customer.CustName, Item.ItemName, Sell.SellQuantity, Sell.TotalAmount FROM Item INNER JOIN (Customer INNER JOIN Sell ON Customer.CustID = Sell.CustID) ON Item.ItemID = Sell.ItemID WHERE POSDate BETWEEN '" & dtpFrom.Text & "' and '" & dtpTO.Text & "' ORDER BY POSDate DESC"
da.SelectCommand.Connection = conn
da.Fill(dt)
Catch ex As Exception
m_lstErrors.Add(New LDError(ex))
dt = Nothing
Finally
da.Dispose()
cmd.Dispose()
End Try
Return dt
dgwSoldItem.DataSource = dt
End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900