Android Question java.lang.NullPointerException

Shahid Saeed

Active Member
Licensed User
Longtime User
I am trying to TAG data in GridView and Retrieving it on ItemClick event. But Getting the following error:-

B4X:
java.lang.NullPointerException
    at it.giuseppe.salvi.gridview.library.core.GridViewActivityWrapper.getTag(GridViewActivityWrapper.java:253)
    at com.prcamels.videos._gridview_itemclick(videos.java:503)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
    at it.giuseppe.salvi.gridview.library.core.GridViewActivityWrapper$1.onItemClick(GridViewActivityWrapper.java:153)
    at it.giuseppe.salvi.gridview.library.core.widget.gridview.GridViewWrapper.onItemClick(GridViewWrapper.java:86)
    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
    at android.widget.AbsListView$1.run(AbsListView.java:3529)


    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException

This is my code:

B4X:
Sub ParseJSON(json As String)
    Dim parser As JSONParser
    parser.Initialize(json)
   
    Dim root As Map = parser.NextObject
    Dim albums As List = root.Get("albums")
    Dim actAlbum As Int = -1   
    For Each col_albums As Map In albums
        actAlbum = actAlbum +1 
        'Dim album_id As String = col_albums.Get("album_id")
        'Dim album_image As String = col_albums.Get("album_image")
        Dim album_title As String = col_albums.Get("album_title")
        fromWeb.Initialize
        If actAlbum = AlbumID Then       
        Dim Videos As List = col_albums.Get("videos")
            Dim vid_row As Int = -1
            For Each album_videos As Map In Videos
            vid_row = vid_row +1
            'Dim picture_id As String = album_pictures.Get("picture_id")
            'Dim picture_date As String = album_pictures.Get("picture_date")
            Dim video_title As String = album_videos.Get("video_title")
            Dim video_image As String = album_videos.Get("video_image")
            Dim video_id As String = album_videos.Get("video_id")
            Dim vidmap As Map
            vidmap.Initialize
            vidmap.Put("video_id",video_id)           
            GridView.Tag = video_id
            GridView.AddImageFromWeb(video_image)
            GridView.SetText(vid_row, video_title)   
            fromWeb.Add(video_image)           
            Next
            GridView.SetArrayList(fromWeb)                   
        Page_title.Text = album_title
        End If       
    Next   
End Sub

Sub GridView_ItemClick(Position As Int, Tag As Object, Value As Object)  
    posSelected = Position
    Dim vid = Sender As PhotoGridView
    Dim vidmap As Map = vid.Tag
    Log(vidmap.Get("video_id"))
    'YouTube(vidmap.Get("video_id"))
   
End Sub
 

Shahid Saeed

Active Member
Licensed User
Longtime User
The error means that the Tag property was not set. I'm not familiar with this library however it seems to me that you are not setting the tag correctly.

What can be done to set the Tag correctly; I am not able to figure out what I am doing wrong? I have also tried this:

B4X:
Dim vidmap As Map
vidmap.Initialize
vidmap.Put("video_id",video_id)           
GridView.Tag = video_id

Sub GridView_ItemClick(Position As Int, Tag As Object, Value As Object)  
    posSelected = Position
    'Dim vid = Sender As PhotoGridView
    Dim vidmap As Map = Tag
    Log(vidmap.Get("video_id"))
    'YouTube(vidmap.Get("video_id"))
    Log(Tag)
   
End Sub

and getting this error:

B4X:
videos_gridview_itemclick (java line: 501)
java.lang.ClassCastException: it.giuseppe.salvi.gridview.library.core.adapter.GridViewAdapterWrapper$ViewHolder cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap


    at com.prcamels.videos._gridview_itemclick(videos.java:501)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
    at it.giuseppe.salvi.gridview.library.core.GridViewActivityWrapper$1.onItemClick(GridViewActivityWrapper.java:153)
    at it.giuseppe.salvi.gridview.library.core.widget.gridview.GridViewWrapper.onItemClick(GridViewWrapper.java:86)
    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
    at android.widget.AbsListView$1.run(AbsListView.java:3529)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.ClassCastException: it.giuseppe.salvi.gridview.library.core.adapter.GridViewAdapterWrapper$ViewHolder cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
You are setting the GridView Tag property. This tag is not specific to any item. I'm not sure how you can set the tag of any specific item. Maybe @lonleystar can help you with this.
I have already asked him before creating this thread but I think he's busy that is why not replying. What can be the other possibilities to pass the video Id of clicked item?
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
I meant that you should add another Map to map between the videos and the tags. Now I'm not familiar with this library so I'm not sure that you have a reference in this event to the relevant key.
If you can give me an example; at-least I can try to implement if it works.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Create a global variable named TagsMap.

Add the tags to the map in this code:
B4X:
vid_row = vid_row +1
            'Dim picture_id As String = album_pictures.Get("picture_id")
            'Dim picture_date As String = album_pictures.Get("picture_date")
            Dim video_title As String = album_videos.Get("video_title")
            Dim video_image As String = album_videos.Get("video_image")
            Dim video_id As String = album_videos.Get("video_id")
            Dim vidmap As Map
            vidmap.Initialize
            vidmap.Put("video_id",video_id)           
            TagsMap.Put(vid_row, video_id)
            GridView.AddImageFromWeb(video_image)
            GridView.SetText(vid_row, video_title)   
            fromWeb.Add(video_image)           
            Next

In the event you can now use Position to get the video_id.
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
Create a global variable named TagsMap.

Add the tags to the map in this code:
B4X:
vid_row = vid_row +1
            'Dim picture_id As String = album_pictures.Get("picture_id")
            'Dim picture_date As String = album_pictures.Get("picture_date")
            Dim video_title As String = album_videos.Get("video_title")
            Dim video_image As String = album_videos.Get("video_image")
            Dim video_id As String = album_videos.Get("video_id")
            Dim vidmap As Map
            vidmap.Initialize
            vidmap.Put("video_id",video_id)          
            TagsMap.Put(vid_row, video_id)
            GridView.AddImageFromWeb(video_image)
            GridView.SetText(vid_row, video_title)  
            fromWeb.Add(video_image)          
            Next

In the event you can now use Position to get the video_id.
Using your example I was getting null for each item but Using your example in a different approach i was able to get individual video id of each item:-

B4X:
Sub Process_Globals
    Dim VideoID_TAG As List
End Sub

Sub ParseJSON(json As String)
    Dim parser As JSONParser
    parser.Initialize(json)
   
    Dim root As Map = parser.NextObject
    Dim albums As List = root.Get("albums")
    Dim actAlbum As Int = -1  
    For Each col_albums As Map In albums
        actAlbum = actAlbum +1        
        Dim album_title As String = col_albums.Get("album_title")
        VideoID_TAG.Initialize
        If actAlbum = AlbumID Then      
        Dim Videos As List = col_albums.Get("videos")
            Dim vid_row As Int = -1
            For Each album_videos As Map In Videos
            vid_row = vid_row +1
            Dim video_title As String = album_videos.Get("video_title")
            Dim video_image As String = album_videos.Get("video_image")
            Dim video_id As String = album_videos.Get("youtube_id")            
            VideoID_TAG.Add(video_id)
            'GridView.Tag = vidmap
            GridView.AddImageFromWeb(video_image)
            GridView.SetText(vid_row, video_title)  
            Next          
        Page_title.Text = album_title
        End If      
    Next  
End Sub

Sub GridView_ItemClick(Position As Int, Value As Object)      
    posSelected = Position    
    Log(Position)
    Log(VideoID_TAG.Get(Position))
    YouTube(VideoID_TAG.Get(Position))      
End Sub
 
Upvote 0
Top