Android Question Error on sorting List created from Array of Types

B4AinNY

Member
Licensed User
I have an array arrX of Typed data items

B4X:
     Public Const g_NumBrands As Int = 101
    Type BrandData(  _
        Distributor As String, _
          Brand As String, _
          ItemTypeExists(18) As Boolean)  
     Public arrX(100) As BrandData

I was looking to be lazy and use build in List sorting capabilities to sort my array
So I added the array elements to the list

B4X:
      Dim L As List
      L.Initialize
      L.addall ( ArrX )

This looks good I verified I have list of typed data with a "Brand" element

B4X:
      Log ("GOT HERE")
      Dim OneRecord As BrandData
      OneRecord = L.Get(1)
      Log ("Brand = " & x.Brand )

So far so good, that works

Now I want to sort this list
B4X:
      ' * Sort the List with  List.Sort(True)
      L.SortType("Brand", True)

I get an error on that last line :

B4X:
     java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.Comparable.compareTo(java.lang.Object)' on a null object reference
    at anywheresoftware.b4a.objects.collections.List$2.compare(List.java:208)
    at java.util.TimSort.binarySort(TimSort.java:261)
    at java.util.TimSort.sort(TimSort.java:204)
    at java.util.TimSort.sort(TimSort.java:169)
    at java.util.Arrays.sort(Arrays.java:2010)

Any ideas ?

I have alternatives, I can write a sorting routine for the array,
but I'm learning B4A and it would be good to know what the problem is.

Thanks,

- Jeff ( B4AinNY )
 

DonManfred

Expert
Licensed User
Longtime User
did you initialized your 100 BrandData items?
 
Upvote 0

B4AinNY

Member
Licensed User
Thanks for the replies.
The problem was that I had Dim'd the BrandData too large.
Once I fixed that it works great.
Probably better off using a database rather than array
but for right now this works.

B4X:
    ' Start with some Array of Types
    Public g_arrData(NumRecords) As BrandData
    '     . . .  .    Make sure NumRecords = Actual number of used records
   
    ' Convert to List      
    Dim L As List
    L.Initialize
    L.addall ( g_arrData )
   
    ' Sort the List with  List.Sort(True)
    L.SortType("Brand", True)
 
    ' If Desired for some reason, put back into array of Types  
     For iRecord = 0 To l.Size - 1
         arrData(i) = L.Get(iRecord)
     Next


- Jeff ( B4AinNY )
 
Upvote 0
Top