B4J Question Little help needed with javaobject

Arie van Wingerden

Member
Licensed User
Running the following code:
Whole non UI program:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

#If JAVA
import java.util.*;
public Map<String, String> GetEnvMap() {
    return System.getenv();
}
#End If

Sub Process_Globals
    Private jo As JavaObject
End Sub

Sub AppStart (Args() As String)
    Log("Before")

    Private envMap As Map = jo.RunMethod("GetEnvMap", Null)
    Log("After")
    For Each k As String In envMap.Keys
        Log(k+"="+ envMap.Get(k) )
    Next
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
gives me:
Waiting for debugger to connect...
Program started.
Before
Error occurred on line: 21 (Main)
java.lang.RuntimeException: Object should first be initialized (JavaObject).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:32)
at anywheresoftware.b4j.object.JavaObject.getCurrentClass(JavaObject.java:259)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
at b4j.example.main._appstart(main.java:80)
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:564)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
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:564)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
at b4j.example.main.main(main.java:30)
Program terminated (StartMessageLoop was not called).

I guess that I have to instantiate variable "jo" first, but my knowledge of both java and b4j is still very limited ...
Thanks for any help ...
 

Arie van Wingerden

Member
Licensed User
Hi Enrique,

I tried to accomplish what you said in the first answer, but it does not work.
Would you be so kind to post my complete code with your suggested changes please?

As for GetSystemProperty I get many hits in the forum, but is there a complete reference for this command? It is not in the basic language manual ...
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
There you have it, but it is not like you thought, the map that returns your inline java doesnt return a compatible b4xMap. so some shortcuts had to be taken.
 

Attachments

  • testSsytem.zip
    1 KB · Views: 141
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It will be easier to help you if you tell us what you are trying to solve.

If you are trying to get an environment variable then use GetEnvironmentVariable:

1593755533108.png
 
Upvote 0

Arie van Wingerden

Member
Licensed User
There you have it, but it is not like you thought, the map that returns your inline java doesnt return a compatible b4xMap. so some shortcuts had to be taken.

Thank you very much!
This does exactly what I intended and I understand what you are doing.
.
So, I understand that it is not possible to map a Java Map directly to a B4J Map.?
 
Upvote 0
Top