3 Replies - 681 Views - Last Post: 14 August 2012 - 12:13 PM Rate Topic: -----

#1 katy_95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-July 12

NullReferenceException was Unhandled

Posted 08 August 2012 - 09:50 PM

Hi,
I'm trying to make a checkout and this is the module I've made. When I try to add anything to the cart, it gives me a "NullReferenceException was Unhandled" error. This error occurs on the row:

For Each objdr In ObjDT.Rows 'For each row in the Data Table

Below is the coding for my module.
**Note: DGCart is the name of my DataGridView

 Module Data
    Public ObjDT As System.Data.DataTable
    Public objdr As System.Data.DataRow
    Public product As String
    Public costcart As Decimal

    Public Sub CreateCart()
        objdt = New system.data.datatable("Cart")
        ObjDT.Columns.Add("ID", GetType(Integer)) 'Creates ID field
        ObjDT.Columns("ID").AutoIncrement = True 'Makes ID field an autonumber
        ObjDT.Columns("ID").AutoIncrementSeed = 1 'Autonumbering starts at 1
        ObjDT.Columns.Add("Product", GetType(String)) 'Second column is called Product. This will list the product e.g. hat, socks
        ObjDT.Columns.Add("Quantity", GetType(Integer)) 'Third column is the quantity being purchased - as a number
        ObjDT.Columns.Add("Cost", GetType(Decimal)) 'Makes fourth column that is cost 
        ShoppingCart.DGCart.DataSource = ObjDT
    End Sub
    Public Sub AddtoCart()
        Dim blnmatch As Boolean = False
        For Each objdr In ObjDT.Rows 'For each row in the Data Table
            If objdr("Product") = product Then
                objdr("Quantity") += ShoppingCart.ItemQuantity.Text
                blnmatch = True
                Exit For
            End If
        Next
        If Not blnmatch Then
            objdr = ObjDT.NewRow
            objdr("Quantity") = ShoppingCart.ItemQuantity.Text
            objdr("Product") = product
            objdr("Cost") = costcart
            ObjDT.Rows.Add(objdr)
        End If
        blnmatch = False

    End Sub

    Function GetTotalCost() As Decimal
        Dim intcounter As Integer
        Dim RunningTotal As Decimal
        intcounter = 0
        For intcounter = 0 To ObjDT.Rows.Count - 1
            objdr = ObjDT.Rows(intcounter)
            RunningTotal += (objdr("Cost") * objdr("Quantity"))
        Next
        Return RunningTotal

    End Function
End Module 

This post has been edited by Martyr2: 08 August 2012 - 10:30 PM
Reason for edit:: Notice closing code tags contain a forward slash "/" :)


Is This A Good Question/Topic? 0
  • +

Replies To: NullReferenceException was Unhandled

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3873
  • View blog
  • Posts: 11,412
  • Joined: 18-April 07

Re: NullReferenceException was Unhandled

Posted 08 August 2012 - 10:37 PM

Well you are trying to loop through rows of your datatable... but have you actually added any on your first add? No, so you don't have any ObjDT.Rows. If there are no rows then ObjDT.rows is going to return null, thus objdr will be null.

This would be my guess as to what is happening. Break point the line and check if objdt.rows is going to return null. If it is, then you know your error.

:)
Was This Post Helpful? 1
  • +
  • -

#3 katy_95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-July 12

Re: NullReferenceException was Unhandled

Posted 13 August 2012 - 05:43 PM

How would I stop the value from being null (add in a new value)?
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3873
  • View blog
  • Posts: 11,412
  • Joined: 18-April 07

Re: NullReferenceException was Unhandled

Posted 14 August 2012 - 12:13 PM

Simply check if there are any rows before you attempt to loop through them as if there were any. if ObjDT.Rows.Count = 0, then just simply add the new row. Will save you some time as well since if there is no rows, why bother going through a loop at all?

Psuedocode...

Check if there are any rows
Yes, loop through them checking if the row already exists
    If row exists, add quantity
    If no, add new row
No, Add new row to empty collection



Hope you get the approach. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1