Android Question error while runnning a standard class in b4xpage

Makumbi

Well-Known Member
Licensed User
Please help i have created this class to run it but it flags this error below. how best can i run this either using resumables sub . because i have tried to run it using resmable subs because it runs brefily then it goes to activity resume i have attached a sample project please help
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Database size = 114688
# of recs in register table: 1
Error occurred on line: 144 (Processdata)
java.lang.NullPointerException: Attempt to read from field 'android.content.Context anywheresoftware.b4a.BA.context' on a null object reference
    at anywheresoftware.b4a.agraham.reflection.Reflection.GetContext(Reflection.java:648)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at b4a.example.b4xmainpage._b4xpage_created(b4xmainpage.java:152)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:318)
    at anywheresoftware.b4a.debug.Debug.CallSubNew2(Debug.java:285)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.keywords.Common.CallSubDebug2(Common.java:1055)
    at b4a.example.b4xpagesmanager._createpageifneeded(b4xpagesmanager.java:1063)
    at b4a.example.b4xpagesmanager._showpage(b4xpagesmanager.java:420)
    at b4a.example.b4xpagesmanager._addpage(b4xpagesmanager.java:248)
    at b4a.example.b4xpagesmanager._addpageandcreate(b4xpagesmanager.java:262)
    at b4a.example.b4xpagesmanager._initialize(b4xpagesmanager.java:168)
    at b4a.example.main._activity_create(main.java:420)
B4X:
Sub Class_Globals
    Private xui As XUI
    Public SQL As SQL
    Dim cursor1 As Cursor
    'Dim checkuser As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    'Return Me
End Sub


Sub checkuser
    'Log(a)
    If IsConnectedToInternet= True Then
        'cursor1 = Starter.SQL1.ExecQuery("SELECT code || phone AS Phone FROM Register")
        Dim SQLQry As String = "DELETE FROM Allacounts"
        B4XPages.MainPage.SQL.ExecNonQuery(SQLQry)
        Dim SQLQry As String = "DELETE FROM student"
        B4XPages.MainPage.SQL.ExecNonQuery(SQLQry)
        Dim SQLQry As String = "DELETE FROM Balances"
        B4XPages.MainPage.SQL.ExecNonQuery(SQLQry)
      
        'Chr(0xF19D)
        'Starter.SQL1.ExecNonQuery("DROP TABLE IF EXISTS Balances")
      
        'Starter.SQL1.ExecNonQuery("CREATE TABLE Balances (Account Text,Names text, Class text,Stream text,Amount REAL, ID INTEGER)")
        cursor1 = B4XPages.MainPage.SQL.ExecQuery("SELECT phone AS Phone,Sex FROM Register")
        If cursor1.RowCount > 0 Then
            '    For i = 0 To cursor1.RowCount - 1
            cursor1.Position =0
            Dim Phoneq As String
            Phoneq = cursor1.Getstring("Phone")
          
            Dim sx As String
            sx = cursor1.Getstring("Sex")
            'Next
        End If
        Dim CustID As String = Phoneq' Customer ID
        Dim jt As HttpJob
        jt.Initialize("", Me)
        jt.Download("http://108.175.14.200/KABOJJAAPPAPI/api/HandlerVBRegistration.ashx?customerid=" & CustID & "&sx=" & sx)
        'Sleep(6000)
        'jt.GetRequest.Timeout = 2000 ' 10 seconds
        Wait For (jt) JobDone(jt As HttpJob)
        If jt.Success Then ' if job is success (http status code 200)
            Dim RetVal As String
            RetVal = jt.GetString
            If jt.GetString = "[]" Then
                'MsgboxAsync("No Records to Upload Yet for: " & CustID ,"SMIS")
                '    Return
                'MsgboxAsync("Your Account is  Not Registered Please Contact the School  for More Details Thank You " ,"SMIS")
      
                '    Return "Not Registered Parent"
                Dim sf As Object = xui.Msgbox2Async("Your Account is  Not Registered Please  Click on Yes Button to Fill the Details of your Child. Please Indicate Child's Name , Class and Stream and other Details Or Contact the School for More Details Thank You", "New Complaint", "Yes", "Cancel", "No",Null)
                Wait For (sf) Msgbox_Result (Result As Int)
                If Result = xui.DialogResponse_Positive Then
                    'Log("Deleted!!!")
                    B4XPages.ShowPage("failedlogin")
                End If
                          
            Else
          
                Dim jpt As JSONParser
                jpt.Initialize(jt.GetString)
                '    Log(jpt) ' will pr
                Dim quotes As List = jpt.NextArray
                For Each quot As Map In quotes
                    Log("Account: " & quot.Get("Account"))
                    Log("Name: " & quot.Get("Name"))
                    Log("Category: " & quot.Get("Category"))
                    Log("Froms: " & quot.Get("Froms"))
                    Log("Tos: " & quot.Get("Tos"))
                    Log(DateTime.Date(JsonDateToTick( quot.Get("Froms"))))
                    Dim l As Long
                    DateTime.DateFormat = "dd/MM/yyyy" ' "1961-08-29"
                    Dim datestring As String = DateTime.Date(JsonDateToTick( quot.Get("Froms")))
                    l = DateTime.DateParse(datestring)
                    DateTime.DateFormat = "dd/MM/yyyy"
                    Log(DateTime.Date(l))
              
                    Log(DateTime.Date(JsonDateToTick( quot.Get("Tos"))))
                    Dim lt As Long
                    DateTime.DateFormat = "dd/MM/yyyy" ' "1961-08-29"
                    Dim datestring As String = DateTime.Date(JsonDateToTick( quot.Get("Tos")))
                    lt = DateTime.DateParse(datestring)
                    DateTime.DateFormat = "dd/MM/yyyy"
                    Log(DateTime.Date(lt))
                    cursor1 = B4XPages.MainPage.SQL.ExecQuery("SELECT ID FROM Allacounts")
                    'cursor1 = SQL1.ExecQuery("SELECT Phone FROM Register")
                    If cursor1.RowCount > 0 Then
                        For i = 0 To cursor1.RowCount - 1
                            cursor1.Position = i
              
                            Dim NewID As Int
                            NewID = cursor1.GetInt("ID")
                        Next

                    End If
          
                    NewID = NewID +1      ' add 1 to the ID number to make a new ID field
  
                    '    Starter.SQL1.ExecNonQuery("INSERT INTO Allacounts VALUES('" & quot.Get("Account") & "','" & CustID  & "','" & NewID & "')")
                    'End If
                    'Grab the last ID number which is the highest number
              
                    B4XPages.MainPage.SQL.ExecNonQuery2("INSERT INTO Allacounts VALUES(?,?,?)", Array As Object(quot.Get("Account"), CustID,NewID))
  
          
                    'trap user from here
              
  
                Next
          
                'Return "Active"
            End If
        End If
        'jt.Release
      
                  
  
    Else
      
        xui.MsgboxAsync("Error Connecting to the Server Please Check Your Internet Connection Or Load Mbs", "Server Error")
      
        't1.Enabled = False

    End If
End Sub


Sub JsonDateToTick(s As String) As Long
    Dim mt As Matcher = Regex.Matcher("\d+", s)
    If mt.Find Then
        Return mt.Match
    End If
    Log("Invalid date: " & s)
    Return 0
End Sub
Sub IsConnectedToInternet As Boolean
    Dim r As Reflector

    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "connectivity", "java.lang.String")
    r.Target = r.RunMethod("getActiveNetworkInfo")

    If r.Target <> Null Then
        Return r.RunMethod("isConnectedOrConnecting")
    End If

    Return False
End Sub

when it reaches here

B4X:
Sub IsConnectedToInternet As Boolean

    Dim r As Reflector



    r.Target = r.GetContext

    r.Target = r.RunMethod2("getSystemService", "connectivity", "java.lang.String")

    r.Target = r.RunMethod("getActiveNetworkInfo")



    If r.Target <> Null Then

        Return r.RunMethod("isConnectedOrConnecting")

    End If



    Return False

End Sub
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Change to:
B4X:
Sub IsConnectedToInternet As Boolean
    Dim ctxt As JavaObject
    Dim NetworkInfo As JavaObject = ctxt.InitializeContext.RunMethodJO("getSystemService", Array("connectivity")).RunMethod("getActiveNetworkInfo", Null)
    If NetworkInfo.IsInitialized Then
      Return NetworkInfo.RunMethod("isConnectedOrConnecting", Null)
    Else
      Return False
    End If
End Sub
 
Upvote 0

Makumbi

Well-Known Member
Licensed User
Change to:
B4X:
Sub IsConnectedToInternet As Boolean
    Dim ctxt As JavaObject
    Dim NetworkInfo As JavaObject = ctxt.InitializeContext.RunMethodJO("getSystemService", Array("connectivity")).RunMethod("getActiveNetworkInfo", Null)
    If NetworkInfo.IsInitialized Then
      Return NetworkInfo.RunMethod("isConnectedOrConnecting", Null)
    Else
      Return False
    End If
End Sub
after running it when it reaches here
B4X:
Dim NetworkInfo As JavaObject = ctxt.InitializeContext.RunMethodJO("getSystemService", Array("connectivity")).RunMethod("getActiveNetworkInfo", Null)
after running it i get this error now


B4X:
** Activity (main) Create, isFirst = true **
Database size = 114688
# of recs in register table: 1
Error occurred on line: 143 (Processdata)
java.lang.NullPointerException: null receiver
    at java.lang.reflect.Field.get(Native Method)
    at anywheresoftware.b4j.object.JavaObject.InitializeContext(JavaObject.java:57)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at b4a.example.b4xmainpage._b4xpage_created(b4xmainpage.java:152)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:318)
    at anywheresoftware.b4a.debug.Debug.CallSubNew2(Debug.java:285)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.keywords.Common.CallSubDebug2(Common.java:1055)
    at b4a.example.b4xpagesmanager._createpageifneeded(b4xpagesmanager.java:1063)
    at b4a.example.b4xpagesmanager._showpage(b4xpagesmanager.java:420)
    at b4a.example.b4xpagesmanager._addpage(b4xpagesmanager.java:248)
    at b4a.example.b4xpagesmanager._addpageandcreate(b4xpagesmanager.java:262)
    at b4a.example.b4xpagesmanager._initialize(b4xpagesmanager.java:168)
    at b4a.example.main._activity_create(main.java:420)
    at java.lang.reflect.Method.invoke(Native Method)
 
Upvote 0
Top