Android Question java.lang.NullPointerException error

jchal

Active Member
Licensed User
Longtime User
hi all i have this code and i try to run my mp3 from my server but i get this error
list.php works but the program stops i cannot understand why the code and php out put is below
please help


B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim mp As MediaPlayerStream
    Type radiopar (First As String, Second As String ,third As String )

End Sub


Sub Globals
    Private BannerAd As AdView
    Private IAd As InterstitialAd
    Dim Label1 As Label
  
    Dim State As Int
    Dim Url As String
    Dim ListView1 As ListView
    Dim Station As String
    Dim UrlDefiner As Int
   
    Dim Button1 As Button
    Dim Button2 As Button
    Dim Button3 As Button
  
    Dim N As Int = (Rnd (0,1) +1)
    Dim L As List
   
    Dim Stream(5) As String
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
 
    Activity.LoadLayout("mysongs")
    ListView1.SingleLineLayout.Label.TextSize = "14"
        If FirstTime Then
        mp.Initialize("mp")
    End If
    Label1.Text = "Click to Listen"
    setupmystream
   

   
End Sub

Sub setupradiostream As Int
    Dim jobadd As HttpJob       
    jobadd.Initialize("mylist", Me)
    jobadd.Download("http://www.mydomain.com/list.php")
    ProgressDialogShow("Downloading Radio List")
End Sub

Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    If Job.Success = True Then
        Dim strReturn As String = Job.GetString
        Dim parser As JSONParser
        parser.Initialize(strReturn)
        If Job.JobName = "mylist" Then
            Dim radiolist As List
       

            radiolist = parser.NextArray 'returns a list with maps
            For i = 0 To radiolist.Size - 1
           
                Dim m As Map
                   
                Log(i)       
                     m = radiolist.Get(i)
                Dim Stream(i) As String
                Dim TL As radiopar 'TwoLines
               
                TL.First = m.Get("rdstream")
                TL.Second = m.Get("rdname")
                TL.third=m.Get ("rdimage")
                Stream(i)=TL.First
                ListView1.AddSingleLine(TL.Second)
               
           
                'Exit
               
            Next
           
           
        Else If Job.JobName = "LogOut" Then
            Dim act As String = parser.NextValue
            If act = "LoggedOut" Then
                ToastMessageShow("Logout successful", True)
                StartActivity(Main)
                Activity.Finish
            End If
        Else
            ToastMessageShow("Error: Invalid Value", True)
        End If
    Else
        'Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
   
    Job.Release
End Sub
Sub IAD_AdClosed
    IAd.LoadAd 'prepare a new ad
End Sub
Sub mp_StreamReady
    Log("starts playing")
    mp.Play
    Label1.Text = "Playing - " & Station
End Sub
Sub mp_StreamError (ErrorCode As String, ExtraData As Int)
    Log("Error: " & ErrorCode & ", " & ExtraData)
    ToastMessageShow("Error: " & ErrorCode & ", " & ExtraData, True)
End Sub
Sub mp_StreamBuffer(Percentage As Int)
    Log(Percentage)
End Sub
Sub Stop_Click
    Log("stop playing")
    mp.Stop
    State = 2
    Label1.Text = "Click a Station to Listen"
    If IAd.Ready Then IAd.Show
End Sub
Sub ListView1_ItemClick (Position As Int, Value As Object)
    Station = Value
    UrlDefiner = Position + 1
    If State = 1 Then Stop_Click
    Url = Stream(UrlDefiner)
    mp.Load(Url)
    State = 1
    Label1.Text = "Buffering"
End Sub
Sub Button1_Click
    If State = 1 Then Stop_Click
End Sub
 

jchal

Active Member
Licensed User
Longtime User
the line is
B4X:
Stream(i)=TL.First

also when i tryed to run it again it gave me a difrent error
 
Upvote 0

Misterbates

Active Member
Licensed User
You have:
B4X:
            For i = 0 To radiolist.Size - 1
                Dim Stream(i) As String
                Stream(i)=TL.First
First time around the for loop you'll execute Dim Stream(0) as String - an array with zero elements. This makes it difficult to access Stream(0), the first element, as there are no elements. To access Stream(0) you need to at minimum Dim Stream(1) as String. When working with arrays, Dim defines the number of elements, which are then numbered from 0 to .Length - 1
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Stream has already been declared in Sub global

Good point, but will probably just be defined locally - as long as this is the desired effect... but probably isn't - it probably needs to be removed from that Sub, so that the global variable is updated.
 
Upvote 0

jchal

Active Member
Licensed User
Longtime User
i did the following changes but still i have errors
i did try to
you need to at minimum Dim Stream(1) as String.


B4X:
Sub Globals
    Dim Stream() As String

End Sub



Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    If Job.Success = True Then
        Dim strReturn As String = Job.GetString
        Dim parser As JSONParser
        parser.Initialize(strReturn)
        If Job.JobName = "radiolist" Then
            Dim radiolist As List
    
            'Dim strOnline As String
            radiolist = parser.NextArray 'returns a list with maps
            For i = 1 To radiolist.Size
        
                Dim m As Map
                
                Log(i)    
                    m = radiolist.Get(i)
                'Dim Stream(i) As String
                Dim TL As radiopar 'TwoLines
            
                TL.First = m.Get("rdstream")
                TL.Second = m.Get("rdname")
                TL.third=m.Get ("rdimage")
                Stream(i)=TL.First
                ListView1.AddSingleLine(TL.Second)
            
        
                'Exit
            
            Next
        
        
        Else If Job.JobName = "LogOut" Then
            Dim act As String = parser.NextValue
            If act = "LoggedOut" Then
                ToastMessageShow("Logout successful", True)
                StartActivity(Main)
                Activity.Finish
            End If
        Else
            ToastMessageShow("Error: Invalid Value", True)
        End If
    Else
        'Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If

    Job.Release
End Sub
i belive the problem is in this line
Stream(i)=TL.First as when i tryed to debug it step by step it gave me
 
Last edited:
Upvote 0

PCastagnetti

Member
Licensed User
Longtime User
I suggest you use a list:

in Sub globals
B4X:
 'Dim Stream(5) As String
             Dim Stream As list

in Activity_Create
B4X:
Stream.Initialize

in JobDone

B4X:
Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    If Job.Success = True Then
        Dim strReturn As String = Job.GetString
        Dim parser As JSONParser
        parser.Initialize(strReturn)
        If Job.JobName = "mylist" Then
            Dim radiolist As List
     

            radiolist = parser.NextArray 'returns a list with maps
           
            For i = 0 To radiolist.Size - 1
         
                Dim m As Map
                 
                Log(i)     
                     m = radiolist.Get(i)
              
                Dim TL As radiopar 'TwoLines
             
                TL.First = m.Get("rdstream")
                TL.Second = m.Get("rdname")
                TL.third=m.Get ("rdimage")
                Stream.add(TL.First)
                ListView1.AddSingleLine(TL.Second)
             
         
                'Exit
             
            Next
         
         
        Else If Job.JobName = "LogOut" Then
            Dim act As String = parser.NextValue
            If act = "LoggedOut" Then
                ToastMessageShow("Logout successful", True)
                StartActivity(Main)
                Activity.Finish
            End If
        Else
            ToastMessageShow("Error: Invalid Value", True)
        End If
    Else
        'Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
 
    Job.Release
End Sub




in ListView1_ItemClick

B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)
    Station = Value
    UrlDefiner = Position + 1
    If State = 1 Then Stop_Click
    Url = Stream.get(UrlDefiner)
    mp.Load(Url)
    State = 1
    Label1.Text = "Buffering"
End Sub
 
Last edited:
Upvote 0

jchal

Active Member
Licensed User
Longtime User
i did the changes you say but i got the following
i get it after
B4X:
Sub setupradiostream As Int
    Dim jobadd As HttpJob       
    jobadd.Initialize("mylist", Me)
    jobadd.Download("http://www.mydomain.com/list.php")
    ProgressDialogShow("Downloading List")
End Sub
 
Upvote 0

PCastagnetti

Member
Licensed User
Longtime User
try to add in jobdone

B4X:
Stream.Initialize  ' -->ADD THIS LINE

For i = 0To radiolist.Size - 1
Dim m AsMap
 Log(i) 
 m = radiolist.Get(i)
 Dim TL As radiopar 'TwoLines 
 TL.First = m.Get("rdstream")
 TL.Second = m.Get("rdname")
 TL.third=m.Get ("rdimage")
 Stream.add(TL.First)
 ListView1.AddSingleLine(TL.Second)
 
 'Exit

 Next
.....
....

setupradiostream is never called in your code
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…