B4J Question HELP Please: Class->Library to send request to API server

FabioAlbaneseTv

Member
Licensed User
Hi
I'm trying to realize a library, starting from a class, to send requests and get answers from my server using API.

This is an example of simple main code (a form with a button)
Main form:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
End Sub

Sub Button1_Click
    Dim myTestApi As test_api_class
    myTestApi.Initialize()
    wait for (myTestApi.GetFromServer("testparm")) complete (Result As Int)
    If myTestApi.GentAnswer<>"" Then
        Log(myTestApi.GentAnswer)
    End If
End Sub

that call this class module called test_api_class

B4X:
Sub Class_Globals
    Dim myHttp             As HttpJob
    Private myAnswer         As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    myAnswer=""
End Sub

public Sub GentAnswer () As String
    Return myAnswer
End Sub

public Sub GetFromServer(pParm As String ) As ResumableSub
    myHttp.Initialize("http",Me)
    myHttp.PostString( "https://www.fabioalbanese.it/api/apitest.php",pParm)
    myHttp.GetRequest()
    Wait For (myHttp) JobDone (myResp As HttpJob)
    If myResp.Success Then
        myAnswer=myResp.GetString
    End If
    Return 1
End Sub

If the class module is inside the project, it works fine.

But, if I compile the class module as s library called "test_api_class" and I add the library to the main project (removing the class module), I obtain this error:

B4X:
Waiting for debugger to connect...
Program started.
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NullPointerException
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:523)
    at anywheresoftware.b4a.keywords.Common.access$0(Common.java:494)
    at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:568)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:140)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:514)
    ... 9 more
Caused by: java.lang.NullPointerException
    at b4j.example.httputils2service._submitjob(httputils2service.java:147)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    ... 10 more

I really don't understand why. The code is the same, the class has the same name
Surely I missed something somewhere, but...
Thanks to anyone can help me
 

FabioAlbaneseTv

Member
Licensed User
Note that you are not releasing the httpjob.

It is much easier and better to work with b4xlibs. Simply zip the modules and change the extension to b4xlib.
Thank you a lot, using the b4xlibs format, it works perfectly.

And... yes, you're right, I had forgotten to release httpjobs ?
 
Upvote 0
Top