B4J Question Error when switching from jHTTPUtils2 to jOkHTTPUtils2

jmon

Well-Known Member
Licensed User
Hello,

I have encountered an error when I switched from jHTTPUtils2 to jOKHTTPUtils2:
Waiting for debugger to connect...
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NullPointerException
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:496)
at anywheresoftware.b4a.keywords.Common.access$0(Common.java:467)
at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:541)
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.ra
iseEvent2(BA.java:120)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:487)
... 9 more
Caused by: java.lang.NullPointerException
at b4j.example.httputils2service._submitjob(httputils2service.java:118)
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:91)
... 10 more
This happens ONLY when compiled as a LIBRARY. When I execute the code not in a Library, it works properly.

The error happens after this:
B4X:
Private Sub GetAccountDetails
    Dim job As HttpJob
    job.Initialize("GetAccountDetails", Me)
    job.Download($"https://api.mailjet.com/v3/REST/user"$)   
    Auth(job)
End Sub

Private Sub Auth(Job As HttpJob)
    Dim su As StringUtils
    Dim userpass As String = $"${MJ_APIKEY_PUBLIC}:${MJ_APIKEY_PRIVATE}"$
    Dim basicAuth As String = su.EncodeBase64(userpass.GetBytes("UTF8"))
    Job.GetRequest.SetHeader("Authorization", "Basic " & basicAuth)   
End Sub
And this error cannot be caught, and seems to happen before "JobDone".

Is there something I'm missing? When I switch back to HTTPUtils2 it works.

Thank you for your help.
 

DonManfred

Expert
Licensed User
make sure to use the library and not the sourcemodules. If you have the modules in your project. Remove the modules from your project. Add okhttp and okhttputils2 libraries to your project.

Edit to add: note that http library should ne removed from project too.

jhttp -> jhttputils2
okhttp -> jokhttputils2
 

jmon

Well-Known Member
Licensed User
Thank you, but my project is already set up like that.

I have managed to make a small example that illustrate the problem, please see the attached files.

  • Open TestLibrary.zip:
This is my example library. When I run this file, everything works as expected, i.e. I get this "ResponseError" message, which is normal because the credentials are wrong:
Waiting for debugger to connect...
Program started.
ResponseError. Reason: , Response:
Job done
  • Compile this file as a library (Alt+5). The library will be called "okHTTPTest" in your additional library folder
  • Open "TestFile.zip"
  • In the library manager, you should see "okHTTPTest" referenced in this file.
  • Run this project in Debug or release. I get this error message:
Waiting for debugger to connect...
Program started.
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NullPointerException
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:496)
at anywheresoftware.b4a.keywords.Common.access$0(Common.java:467)
at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:541)
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:120)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:487)
... 9 more
Caused by: java.lang.NullPointerException
at b4j.example.httputils2service._submitjob(httputils2service.java:118)
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:91)
... 10 more
It seems that I cannot compile jOKHTTPUtils2 as a library?
 

Attachments

jmon

Well-Known Member
Licensed User
Simplest solution is to make the other library a b4xlib as well. It is trivial to do.
Ok, that worked. I zipped the "TESTHTTP.bas" as .zip. Renamed the extension to .b4xlib" and it worked. Over the past year I missed many new features! Thank you for your help.

So that lead me to a feature request: Maybe have the option to compile the libraries as b4xLib or jar?
 

Erel

Administrator
Staff member
Licensed User
Maybe have the option to compile the libraries as b4xLib or jar?
It was discussed. Once you start creating b4xlib, especially cross platform ones, you will quickly see that the library is made of all kinds of folders and files and a simple batch file is more appropriate for this task.

Example of the batch file that creates B4XCollections lib:
B4X:
del "B4XCollections.b4xlib"
..\..\zip "B4XCollections.b4xlib" -S "*.bas" "manifest.txt"
copy "B4XCollections.b4xlib" C:\Users\H\Documents\AdditionalLibs\B4X
A more complicated one for XUI Views:
B4X:
del "XUI Views.b4xlib"
cd "XUI Views"
copy B4A\Files\*.* Files\
copy B4i\Files\*.* Files\
copy B4J\Files\*.* Files\
del Files\1.*
del Files\2.*
..\..\zip "..\XUI Views.b4xlib" -S "*.*" -S Files\*.* -S B4J\B4JTextFlow.bas
Zip utility: http://infozip.sourceforge.net/
 
Top