Error when sorting a collection

Posts   
 
    
JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 22-Nov-2012 13:33:16   

I'm getting an error when sorting a collection. It seems consistent in throwing this error in the code where it lies but it's not repeatable when I try to do it in a seperate piece of code and I can't for the life of me work out why.

The line in question is:

TravelCollection(grdTravelSectors.SelectedItems(0).GetRow.RowIndex).JourneyPaxLink.Sort(JourneyPaxLinkFields.Active.FieldIndex, System.ComponentModel.ListSortDirection.Descending)

The error that is getting thrown is a NullReferenceException - Object reference not set to an instance of an object.

I'm using VB.NET in visual studio 2010 and using LLBLGen Pro 2.6 (I tried upgrading a while ago but have a few tables with dual primary keys that aren't in alphabetical order so decided it was too much work to upgrade when everything was working)

I was targetting .NET 2.0 until recently when I switched to .NET 4.0 - this error didn't start coming up straight away so I don't think it's related to the .NET framework change but you never know.

I've tried running this test code in a different area and this works fine so I just can't see what's going wrong...

Dim TravelCollection As New TravBase.CollectionClasses.ItinerarySectorCollection
TravelCollection.Add(New ItinerarySectorEntity(1536))
TravelCollection.Add(New ItinerarySectorEntity(174))

TravelCollection(0).JourneyPaxLink.Sort(JourneyPaxLinkFields.Active.FieldIndex, _
                                        System.ComponentModel.ListSortDirection.Descending)
TravelCollection(0).JourneyPaxLink.Sort(JourneyPaxLinkFields.LeadPassenger.FieldIndex, _
                                        System.ComponentModel.ListSortDirection.Ascending)

The form where this is happening does an awful lot but it doesn't write anything before it gets to this error.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 23-Nov-2012 01:00:06   

Did you place a break point to see which object is null (Nothing).

I bet it has something to do with: grdTravelSectors.SelectedItems(0).GetRow.RowIndex Maybe sometimes there is no selected rows.

Can you move this line above the other one, and see if it returns a value, or an exception?

JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 23-Nov-2012 10:06:52   

I did put a breakpoint there and that's the strange thing - I can quick view TravelCollection(grdTravelSectors.SelectedItems(0).GetRow.RowIndex).JourneyPaxLink and it shows a JourneyPaxLink collection with, in the example I first saw the problem with, .count = 11 (See attached image)

JourneyPaxLinkFields.Active.FieldIndex is 5 and the value against System.ComponentModel.ListSortDirection.Descending is 1

I added some more code:

Try
    TravelCollection(grdTravelSectors.SelectedItems(0).GetRow.RowIndex).JourneyPaxLink.Sort(JourneyPaxLinkFields.Active.FieldIndex, System.ComponentModel.ListSortDirection.Descending)
    TravelCollection(grdTravelSectors.SelectedItems(0).GetRow.RowIndex).JourneyPaxLink.Sort(JourneyPaxLinkFields.LeadPassenger.FieldIndex, System.ComponentModel.ListSortDirection.Ascending)
Catch ex As Exception
    Try
        Dim TempItin As ItinerarySectorEntity = TravelCollection(grdTravelSectors.SelectedItems(0).GetRow.RowIndex)
        TempItin.JourneyPaxLink.Sort(JourneyPaxLinkFields.Active.FieldIndex, System.ComponentModel.ListSortDirection.Descending)
        TempItin.JourneyPaxLink.Sort(JourneyPaxLinkFields.LeadPassenger.FieldIndex, System.ComponentModel.ListSortDirection.Ascending)
        TravelCollection(grdTravelSectors.SelectedItems(0).GetRow.RowIndex) = TempItin
    Catch ex2 As Exception
        Dim TempItin As New ItinerarySectorEntity(TravelCollection(grdTravelSectors.SelectedItems(0).GetRow.RowIndex).ItinSectorId)
        TempItin.JourneyPaxLink.Sort(JourneyPaxLinkFields.Active.FieldIndex, System.ComponentModel.ListSortDirection.Descending)
        TempItin.JourneyPaxLink.Sort(JourneyPaxLinkFields.LeadPassenger.FieldIndex, System.ComponentModel.ListSortDirection.Ascending)
        TravelCollection(grdTravelSectors.SelectedItems(0).GetRow.RowIndex) = TempItin
    End Try
End Try

The first sort throws an error, the second sort throws an error (caught as ex2), the third sort, where it gets a new instance of the same entity object, sorts without any problem.

Attachments
Filename File size Added on Approval
Error.PNG 59,224 23-Nov-2012 10:07.04 Approved
JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 23-Nov-2012 11:47:09   

I've attached another screenshot here with further details - it does look like it could be something to do with the fact that the data is bound to a grid.

I had forgotten that I'd updated to a new version of a different dll so this was nothing to do with the LLBLGen stuff and it's the new version of that software that's caused me a two day headache!

Thanks as always for your help.

Attachments
Filename File size Added on Approval
MoreErrorDetail.png 38,491 23-Nov-2012 11:47.20 Approved
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 24-Nov-2012 10:19:16   

Glad it's sorted! smile

Frans Bouma | Lead developer LLBLGen Pro