Android Question [SOLVED] - [B4X] CLVSelections null pointer error when trying to set the selection mode

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

I'm trying out this class in B4A and get a null pointer error when trying to set the selection mode. The coding is based on the sample code from where I got the class from.

B4X:
java.lang.NullPointerException: Attempt to invoke virtual method 'int anywheresoftware.b4a.objects.collections.List.getSize()' on a null object reference

B4X:
Sub InitializeWhatsNeeded

    strShared = rp.GetSafeDirDefaultExternal("Settings")
    kvs.Initialize(strShared, "Settings")
    CSelections.Initialize(clvSongList)
    CSelections.Mode = CSelections.MODE_MULTIPLE_ITEMS
End Sub

Sub clvSongList_ItemClick (Index As Int, Value As Object)

    CSelections.ItemClicked(Index)
    kvs.Put("CurrentSongSelected", Index +1)
    Dim iv As typListItemValues = Value
    Log("You clicked song title: " & iv.LabelSongTitle & "   " & iv.strSomeMoreText)
End Sub

The class came from here.
 

rleiman

Well-Known Member
Licensed User
Longtime User
Please post the full error message.
Are you sure that clvSongList was initialized before CSelections was initialized?
Good catch. šŸ‘
I think I read somewhere that loading a layout initializes all the views on the layout and clvSongList was on the SongList layout. I moved the coding to the area that builds the custom list view and no crash.

B4X:
Sub ImageViewSongList_Click

    Activity.LoadLayout("SongList") ' This layout also contains a custom list view.

    CSelections.Initialize(clvSongList)
    CSelections.Mode = CSelections.MODE_MULTIPLE_ITEMS

    ' Make panel slide down out of sight.
    '-----------------------------------------
    PanelMenu.SetLayoutAnimated(300, 0, Activity.Height, Activity.Width, _
        Activity.Height)

    For intIndex = 0 To 2
        
        ' typValues will be used to store values into the Type elements for the list views.
        '----------------------------------------------------------------------------------
        Dim typValues As typListItemValues

        ' Adding items to the list view requires a Panel created in code.
        '----------------------------------------------------------------
        Dim p As B4XView = xui.CreatePanel("")

        ' This is the size for the panel and it will be animated.
        '--------------------------------------------------------
        p.SetLayoutAnimated(0, 0, 0, PanelSongList.Width, 200dip)

        p.LoadLayout("SongListCard")

        ' Populate the views with values from the database.
        '--------------------------------------------------       
        Select intIndex
            Case 0
                LabelSongTitle.Text = kvs.Get("Song1Title")
                LabelSchedule.Text = kvs.Get("Song1Schedule")
                
                If kvs.Get("Song1ScheduleIsActivated") Then
                    ImageViewSongIsActivated.Bitmap = _
                        LoadBitmap(File.DirAssets, "light-on-icon.png")
                Else
                    ImageViewSongIsActivated.Bitmap = _
                        LoadBitmap(File.DirAssets, "light-off-icon.png")
                End If
                
                typValues.strSomeMoreText = "One"
                
            Case 1
                LabelSongTitle.Text = kvs.Get("Song2Title")
                LabelSchedule.Text = kvs.Get("Song2Schedule")

                If kvs.Get("Song2ScheduleIsActivated") Then
                    ImageViewSongIsActivated.Bitmap = _
                        LoadBitmap(File.DirAssets, "light-on-icon.png")
                Else
                    ImageViewSongIsActivated.Bitmap = _
                        LoadBitmap(File.DirAssets, "light-off-icon.png")
                End If
                
                typValues.strSomeMoreText = "Two"

            Case 2
                LabelSongTitle.Text = kvs.Get("Song3Title")
                LabelSchedule.Text = kvs.Get("Song3Schedule")

                If kvs.Get("Song3ScheduleIsActivated") Then
                    ImageViewSongIsActivated.Bitmap = _
                        LoadBitmap(File.DirAssets, "light-on-icon.png")
                Else
                    ImageViewSongIsActivated.Bitmap = _
                        LoadBitmap(File.DirAssets, "light-off-icon.png")
                End If
                
                typValues.strSomeMoreText = "Three"
        End Select

        typValues.LabelSongTitle = LabelSongTitle.Text
        ImageViewPlayPauseSong.Tag = intIndex
        
        ' This is where the item row is added to the list view.
        ' Each list item has a set of Type structure elements associated with it.
        '------------------------------------------------------------------------
        clvSongList.Add(p,typValues)
    Next
End Sub
 
Upvote 0
Top