B4J Question java.lang.NullPointerException and Resumable Sub

Star-Dust

Expert
Licensed User
Longtime User
I created a class that has a method inside which is resumable sub.
If I insert the class code on one of my applications it works perfectly. If I compile it as a library and insert it into the same Application it generates this error

httputils2service._submitjob (java line: 147)
java.lang.NullPointerException
at b4j.example.httputils2service._submitjob(httputils2service.java:147)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:504)
at anywheresoftware.b4a.keywords.Common.access$0(Common.java:484)
at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:558)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)

My app source is the same and calls the class in the same way
B4X:
Private Sub ButtonConnect_Click
    MyClient.Initialize
    Wait For (MyClient.Connect(TextField1.Text,TextField2.Text,TextField3.Text,TextField4.Text,MyBoolean)) Complete (Success As Boolean)
    Here it generates the error without even entering the sub. I put a log at the beginning of the sub and it doesn't even appear.
    log(Success)
End Sub

This is roughly the content of the method
B4X:
Public Sub Connect(S1 As String,S2 As String, S3 As String, S4 As String, MyBool As String) As ResumableSub
    Log("First") ' This log does not even appear
    client.Initialize("",Me)
    client.Download("https://www.myserver.xx")
    client.GetRequest.SetHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36")
    Wait For (client) JobDone(j As HttpJob)
    if j.success=False  then return False

    Log("Second pass")
    client.Download("https://www.myserver.xx/second/")
    client.GetRequest.SetHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36")
    Wait For (client) JobDone(j As HttpJob)

    if j.success=False  then return False
    Return true
End SUb

If instead of as a library I insert it as a class everything works correctly.
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
b4xlib or jar? You will need to create a b4xlib as OkHttpUtils2 is a b4xlib.
it's jar. I have to create Jar on this occasion.

So do you think it is related to OkHttpUtils2?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
b4xlibs cannot be referenced from compiled jars. Simple solution is to create a b4xlib.

If you must use a jar then you will need to either use an old version of OkHttpUtils2 or reference OkHttpUtils2 from the project that uses the library. The later is not so simple as you will need to make the relevant calls in an indirect way (CallSub to a code module that will access HttpJob).
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
b4xlibs cannot be referenced from compiled jars. Simple solution is to create a b4xlib.

If you must use a jar then you will need to either use an old version of OkHttpUtils2 or reference OkHttpUtils2 from the project that uses the library. The later is not so simple as you will need to make the relevant calls in an indirect way (CallSub to a code module that will access HttpJob).
I believed that B4xLib were compiled inside the JAR as if they were internal classes and sources. I didn't think they were referencing.

Could you also compile okHttpUtils2 in Jar to be able to reference it?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I believed that B4xLib were compiled inside the JAR as if they were internal classes and sources. I didn't think they were referencing.
This will not work as you will very soon have the same classes multiple times.

Could you also compile okHttpUtils2 in Jar to be able to reference it?
Yes, but it will force anyone who uses your library to use the xml / jar library instead of the b4xlib.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Yes, but it will force anyone who uses your library to use the xml / jar library instead of the b4xlib.
Sure. They will not be able to use the original one unless I change my name when I fill it out.

Thanks for the explanations
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Good evening @Erel,

The library I created is B4X. Today I wanted to compile the B4A version in JAR and it does not raise this error which instead is raised in B4J.
Instead, this message appears: sending message to waiting queue of uninitialized activity (submitjob)

I find it strange. Do you have an explanation?
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Thank you Erel. I am looking for a simple solution. since other compiled libraries I have managed to wrap okhttputils2, there is probably some difference and I would like to identify it
 
Upvote 0
Top