Android Question Try-Catch not working as expected?

ac9ts

Active Member
Licensed User
Longtime User
I have an app that the number 1 crash report is:

java.lang.NullPointerException:

at anywheresoftware.b4a.keywords.Common.LastException (Common.java:810)
at com.SudzDev.GAOR.main._timer1_tick (main.java:3448)
at java.lang.reflect.Method.invoke (Native Method)
at anywheresoftware.b4a.BA.raiseEvent2 (BA.java:186)
at anywheresoftware.b4a.objects.Timer$TickTack.run (Timer.java:105)
at android.os.Handler.handleCallback (Handler.java:751)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6123)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:757)

There are a few different scenarios that might cause this but I haven't had the time to re-write the code so I put a Try/Catch around the whole sub. I still get crash reports and was wondering why it wasn't getting trapped? Another part of the question is, are the users seeing the crash or is it just being reported in the background and the app continues to run?

The sub code is:
B4X:
Sub Timer1_Tick

    Try
        If CurrentScreen = "Main" Then
          
            If Player.CurrentPosition > 0  Then
                  
                ' Normal play mode
                If NewPosition = 0 Then
                    is1.Visible(True)
                    is1.SetMaxVal(Player.Duration)
                    is1.SetPos(Player.CurrentPosition/Player.Duration)
                Else
                    ' User is seeking in the stream so don't update until within 2 seconds of target
                    If Abs(NewPosition-Player.CurrentPosition) < 2 * DateTime.TicksPerSecond Then NewPosition = 0
                End If
              
                Playing = True
                NowPlaying
            Else
                is1.Visible(False)
                Playing = False
                NowPlaying
            End If
        End If
      
        If DispType = "Status" Then        ' Update the download status
            Dim L1 As String
            Dim L2 As String
            Dim RtnVal As String
          
            lstSelect.Clear
            SetListPrompt
          
            If DownloadService.jobs.IsInitialized Then
                For Each job As HttpJob In DownloadService.jobs.Values
                    Dim jt As JobTag = job.Tag
                    If jt.CountingStream.IsInitialized And jt.CountingStream.Count > 0 Then
                        L1 = jt.Data.DispName
                        L2 = NumberFormat(jt.CountingStream.Count / 1024, 0, 0) & "KB / " & NumberFormat(jt.Total / 1024, 0, 0) & "KB"
                        RtnVal = jt.Data.url
                        lstSelect.Addtextitem(L1 & CRLF & L2, RtnVal)
                    End If
                Next
            End If
        End If
      
        If pnlSplash.IsInitialized And pnlSplash.Visible Then        ' Update the content.db download status

            Dim DL_Prog As Int
          
            Try
                For Each job As HttpJob In DownloadService.jobs.Values
                    Dim jt As JobTag = job.Tag
                    If jt.CountingStream.IsInitialized And jt.CountingStream.Count > 0 And jt.Data.DestName = "Content.db" Then
                  
                        DL_Prog = (jt.CountingStream.Count/jt.Total) * 100
                        If DL_Prog>100 Then DL_Prog=100
                        prgLoadContent.Progress = DL_Prog
                    End If
                Next
            Catch
                LogColor(LastException, Colors.Red)
            End Try
                  
            End If
    Catch
        LogColor("Timer1_Tick Error!", Colors.Red)
    End Try
  
End Sub
 

ac9ts

Active Member
Licensed User
Longtime User
It is a process global in the main activity. It does get disabled in Activity_Pause.
 
Upvote 0
Top