Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Hi there! I need some help. I don't know why I'm having this error. Please help me. This is my code:
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
Error:
"object reference not set to an instance of an object"
 
Thanks!!
Posted 10-Oct-12 19:41pm
Edited 11-Oct-12 0:36am
v4
Comments
AshishChaudha at 11-Oct-12 1:47am
   
Have you debugged your code..?
Janna Dela Cruz at 11-Oct-12 1:49am
   
yeah.
AshishChaudha at 11-Oct-12 1:52am
   
Please specify the line exactly where you are getting the exception.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If the error really happens in the line you say, then Total_PerItem is null.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

First Check all datatables and Comparison for Nothing.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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:
 
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:
 For Each total As DataRow In Total_PerItem.Rows
 
Second thing is this part:
 
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:
 
item.Item("ItemCode")  ' ItemCode must be in DataTable
 
I hope it can help you to figure out problem.
  Permalink  
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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
  Permalink  

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

  Print Answers RSS
0 Dnyaneshwar@Pune 604
1 thatraja 370
2 OriginalGriff 329
3 Sergey Alexandrovich Kryukov 290
4 CPallini 219
0 OriginalGriff 697
1 Dnyaneshwar@Pune 604
2 Kornfeld Eliyahu Peter 495
3 thatraja 370
4 Sergey Alexandrovich Kryukov 344


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 18 Feb 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100