System.NullReferenceException was unhandled

paulthepaddy

Well-known member
Joined
Apr 9, 2011
Messages
222
Location
UK
Programming Experience
Beginner
System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=Image Invoice Management
StackTrace:
at Image_Invoice_Management.Form_Invoice_Management.SSR_Scratches(CheckedItemCollection Damage, car SelectCar) in C:\Users\Paul\Documents\VB Stuff\Projects\Image Invoice Management b test Object List\Image Invoice Management\Invoice Managment.vb:line 858
at Image_Invoice_Management.Form_Invoice_Management.BTN_Add_Damage_Click(Object sender, EventArgs e) in C:\Users\Paul\Documents\VB Stuff\Projects\Image Invoice Management b test Object List\Image Invoice Management\Invoice Managment.vb:line 600
`

hey guys this is the error im getting and this is what is causing it i have colour coded it so it is easier to read... i hope

VB.NET:
[COLOR=#0000ff]Public Sub[/COLOR] SSR_Scratches(Damage [COLOR=#0000ff]As[/COLOR] [COLOR=#40e0d0]CheckedListBox.CheckedItemCollection[/COLOR], SelectCar [COLOR=#0000ff]As[/COLOR] [COLOR=#40e0d0]car[/COLOR])
        Car_Damage_Type = [COLOR=#ff0000]"Repair Scratches on "[/COLOR]
       [COLOR=#0000ff]Dim[/COLOR] Start_Pos [COLOR=#0000ff]As[/COLOR] Integer = SelectCar.SSR_ScratchDamage.Count[COLOR=#008000]' Error Here[/COLOR]

        [COLOR=#008000]'Cases If only Adding 1 Item To Damagelist[/COLOR]
[COLOR=#0000ff]Select Case[/COLOR] Damage.Count

            [COLOR=#008000] 'Case For adding 1 Item into an Empty List[/COLOR]
            [COLOR=#0000ff]Case Is[/COLOR] = 1
                [COLOR=#0000ff]Select Case[/COLOR] SelectCar.SSR_ScratchDamage.Count
                    [COLOR=#0000ff]Case Is[/COLOR] = 0
                        SelectCar.SSR_ScratchDamage(Start_Pos) = Car_Damage_Type & Damage.Item(0)
                        SelectCar.Subtotal += 25

                       [COLOR=#008000] 'Case For adding 1 Item into a NON Empty List[/COLOR]
                   [COLOR=#0000ff] Case Is[/COLOR] >= 1
                        [COLOR=#0000ff]If Not[/COLOR] SelectCar.SSR_ScratchDamage.Contains(Damage.Item(0)) [COLOR=#0000ff]Then[/COLOR]
                            SelectCar.SSR_ScratchDamage(Start_Pos) = Damage.Item(0)
                            SelectCar.Subtotal += 15
                       [COLOR=#0000ff] End If
                End Select[/COLOR]

               [COLOR=#008000] 'Cases For Adding More Than 1 Item To Damagelist[/COLOR]
            [COLOR=#0000ff]Case Is >= 2[/COLOR]
                
                [COLOR=#0000ff]Select Case[/COLOR] SelectCar.SSR_ScratchDamage.Count
                   [COLOR=#008000]'Case For Adding Multiple Items Into a Emply List[/COLOR]
                    [COLOR=#0000ff]Case Is[/COLOR] = 0
                        SelectCar.SSR_ScratchDamage(Start_Pos) = Car_Damage_Type & Damage.Item(0)
                        [COLOR=#0000ff]For[/COLOR] i = 1 To Damage.Count - 1
                           [COLOR=#0000ff] If Not[/COLOR] SelectCar.SSR_ScratchDamage.Contains(Damage.Item(i)) [COLOR=#0000ff]Then[/COLOR]
                                SelectCar.SSR_ScratchDamage(Start_Pos) = Damage.Item(i)
                                Start_Pos = Start_Pos + 1
                           [COLOR=#0000ff] End If
                        Next[/COLOR]
                        SelectCar.Subtotal += ((Damage.Count * 15) + 10)


                       [COLOR=#008000] 'case For Adding Multiple Item into a Non Empty List[/COLOR]
                    [COLOR=#0000ff]Case Is[/COLOR] >= 1
                        [COLOR=#0000ff]For[/COLOR] i = 0 To Damage.Count - 1
                           [COLOR=#0000ff] If Not[/COLOR] SelectCar.SSR_ScratchDamage.Contains(Damage.Item(i)) [COLOR=#0000ff]Then[/COLOR]
                                SelectCar.SSR_ScratchDamage(Start_Pos) = Damage.Item(i)
                                Start_Pos = Start_Pos + 1
                            [COLOR=#0000ff]End If
                        Next[/COLOR]
                        SelectCar.Subtotal += ((Damage.Count * 15) + 10)
                [COLOR=#0000ff]End Select
        End Select
        For Each[/COLOR] item [COLOR=#0000ff]In[/COLOR] SelectCar.SSR_ScratchDamage
            CheckedListBox_Car.Items.Add(item)
        [COLOR=#0000ff]Next
    End Sub[/COLOR]

i Dont understand why this is giving an error i call the sub using this
VB.NET:
Call SSR_Scratches(Damage_List.CheckedItems, ComboBox_Current_Car.SelectedItem)

the ComboBox_Current_Car.SelectedItem contains a BindingList(Of car) = New BindingList(Of car)

i do understand that the list count is 0 so shouldn't it be declairing Start_Pos = 0

thanks for any advice in advanced and if any1 knows of a better way to write the sub please do inform
 
If the Count is zero then the collection has no items, so it has no indexes, so how can you set the item at any index at all? If you want to add a new item to the collection then you call the Add method.
 
i understand that the collection is nothing as teir isn't anything in it but how can i code it so if the collection = nothing the do the case i need.. i tried using an if statement and set the values into a varriable then use the varriable as the case but again this error showed

System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
[CODEIf SelectCar.SSR_ScratchDamage.Count = 0 Then
itemcount = 0
ElseIf SelectCar.SSR_ScratchDamage.Count > 0 Then
itemcount = 1
End If[/CODE]


VB.NET:
[COLOR=#0000ff]Public Sub[/COLOR] SSR_Scratches(Damage [COLOR=#0000ff]As[/COLOR] [COLOR=#40e0d0]CheckedListBox.CheckedItemCollection[/COLOR], SelectCar [COLOR=#0000ff]As[/COLOR] [COLOR=#40e0d0]car[/COLOR])
        Car_Damage_Type = [COLOR=#ff0000]"Repair Scratches on "[/COLOR]

        [COLOR=#008000]'Cases If only Adding 1 Item To Damagelist[/COLOR]
[COLOR=#0000ff]Select Case[/COLOR] Damage.Count

            [COLOR=#008000] 'Case For adding 1 Item into an Empty List[/COLOR]
            [COLOR=#0000ff]Case Is[/COLOR] = 1
                [COLOR=#0000ff]Select Case[/COLOR] SelectCar.SSR_ScratchDamage.Count
                    [COLOR=#0000ff]Case Is[/COLOR] = 0
                        SelectCar.SSR_ScratchDamage.Add(Car_Damage_Type & Damage.Item(0))
                        SelectCar.Subtotal += 25

                       [COLOR=#008000] 'Case For adding 1 Item into a NON Empty List[/COLOR]
                   [COLOR=#0000ff] Case Is[/COLOR] >= 1
                        [COLOR=#0000ff]If Not[/COLOR] SelectCar.SSR_ScratchDamage.Contains(Damage.Item(0)) [COLOR=#0000ff]Then[/COLOR]
                            SelectCar.SSR_ScratchDamage.Add(Damage.Item(0))
                            SelectCar.Subtotal += 15
                       [COLOR=#0000ff] End If
                End Select[/COLOR]

               [COLOR=#008000] 'Cases For Adding More Than 1 Item To Damagelist[/COLOR]
            [COLOR=#0000ff]Case Is >= 2[/COLOR]
                
                [COLOR=#0000ff]Select Case[/COLOR] SelectCar.SSR_ScratchDamage.Count
                   [COLOR=#008000]'Case For Adding Multiple Items Into a Emply List[/COLOR]
                    [COLOR=#0000ff]Case Is[/COLOR] = 0
                        SelectCar.SSR_ScratchDamage.Add(Car_Damage_Type & Damage.Item(0))
                        [COLOR=#0000ff]For[/COLOR] i = 1 To Damage.Count - 1
                           [COLOR=#0000ff] If Not[/COLOR] SelectCar.SSR_ScratchDamage.Contains(Damage.Item(i)) [COLOR=#0000ff]Then[/COLOR]
                                SelectCar.SSR_ScratchDamage.Add(Damage.Item(i))
                           [COLOR=#0000ff] End If
                        Next[/COLOR]
                        SelectCar.Subtotal += ((Damage.Count * 15) + 10)

                       [COLOR=#008000] 'case For Adding Multiple Item into a Non Empty List[/COLOR]
                    [COLOR=#0000ff]Case Is[/COLOR] >= 1
                        [COLOR=#0000ff]For[/COLOR] i = 0 To Damage.Count - 1
                           [COLOR=#0000ff] If Not[/COLOR] SelectCar.SSR_ScratchDamage.Contains(Damage.Item(i)) [COLOR=#0000ff]Then[/COLOR]
                                SelectCar.SSR_ScratchDamage.Add(Damage.Item(i))
                            [COLOR=#0000ff]End If
                        Next[/COLOR]
                        SelectCar.Subtotal += ((Damage.Count * 15) + 10)
                [COLOR=#0000ff]End Select
        End Select
        For Each[/COLOR] item [COLOR=#0000ff]In[/COLOR] SelectCar.SSR_ScratchDamage
            CheckedListBox_Car.Items.Add(item)
        [COLOR=#0000ff]Next
    End Sub[/COLOR]
 
NullReferenceException said:
Dim Start_Pos As Integer = SelectCar.SSR_ScratchDamage.Count' Error Here
So either SelectCar or SSR_ScratchDamage is Nothing.
SelectCar was passed from combobox SelectedItem which is Nothing if no item is selected, this can be checked before making that call.
If it's not that is must be SSR_ScratchDamage, which seems to be a collection, in that case you would have to see if that collection has not been created.
 
the SelectCar isn't nothing as it is a bindinglist of car and on the load event car 1 is created and the default selected item for the combobox.

but the SSR_ScratchDamage IS nothing my problem is i need a way to see if the collection is nothing for the select case. their are 4 different senarios

notes:
Damage is a Checkedlistbox with checked items
The list im refering to is the SSR_Scratches

1) Add 1 Damage to an empty list (needs to add damage description into the top of the list as well as the Damage plus the price for single is diferent from multiple damage)
2)Add More than 1 Damage into an empty list (again needs to ass description and price formula is different)

3)Add 1 Damage Into a Non-Empty list
4)Add more than 1 damage insto a Non-Empty list

i was doing

VB.NET:
Damage [COLOR=#0000ff]As[/COLOR] [COLOR=#40e0d0]CheckedListBox.CheckedItemCollection
[/COLOR]Selectcar.SSR_Scratches.count is a list in a car class
[COLOR=#40e0d0]
[/COLOR]Select Case Damage

Caseis =0 (1 Damage)

         Select Case Selectcar.SSR_Scratches.count
               
                 Caseis = 0 (add 1 damage into an empty list)
 
                 Caseis >=1 (add 1 damage into Non Empty List)

caseis >=1 (more than 1)

Select Case Selectcar.SSR_Scratches.count
               
                 Caseis = 0 (add more than 1 damage into an empty list)
 
                 Caseis >=1 (add More than 1 damage into Non Empty List)

end case[COLOR=#40e0d0]

[/COLOR]

i hope i have made it a bit clearer as to what i want to achive as no one is telling me a way around the collection null error im guessing their isn't a easier way to do it.
can any1 give me a clue as to what i should read up aboput that would let me achive what i want
 
OMG sorry guys i just realise when i was declairing the list in the class i was declairing

Public SSR_ScratchDamage As List(Of String)
instead of
Public SSR_ScratchDamage As List(Of String) = New List(Of String)

it just clicked to me when johnH said about checking weather the collection was created

sorry to bother you guys with something so silly
 
Back
Top