B4J Question unable to initialize objects by CallSub

Alessandro71

Well-Known Member
Licensed User
Longtime User
having to manage a collection of different objects, i'm trying to call the Initialize sub via CallSub
here is a minimal proof-of-concept (useless) code

B4X:
Sub AppStart (Args() As String)
    Dim o1 As Class1
    Dim o2 As Class2
    Dim a() As Object = Array As Object(o1, o2)

    For Each o As Object In a
        CallSub(o, "Initialize")
    Next
End Sub

This code throws the following exception

Rich (BB code):
main._appstart (java line: 58)
java.lang.RuntimeException: java.lang.NullPointerException
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:522)
    at anywheresoftware.b4a.keywords.Common.CallSubNew(Common.java:460)
    at b4j.example.main._appstart(main.java:58)
    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:111)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at b4j.example.main.main(main.java:28)
Caused by: java.lang.NullPointerException
    at b4j.example.class1.callSub(class1.java:41)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:497)
    ... 9 more

is Initialize forbidden to be called by CallSub?
 
Solution
you could change your loop to (using javaobject)
B4X:
    For Each o As Object In a
       
    #if debug
' debug needs 2 parameters
    o.As(JavaObject).RunMethod("_initialize",Array(Null,Null))
    #else
' release only needs 1
    o.As(JavaObject).RunMethod("_initialize",Array(Null))
    #end if   
    Next

My full working code (Class1 is just a bare class)
B4X:
Sub Process_Globals
    Dim a As Class1
    Dim b As Class1
    Dim c() As Object = Array As Object(a,b)
End Sub

Sub AppStart (Args() As String)
    
    Log(a.IsInitialized)
    Log(b.IsInitialized)

    For Each o As Object In c
        #if debug
            o.As(JavaObject).RunMethod("_initialize",Array(Null,Null))
        #else...

Daestrum

Expert
Licensed User
Longtime User
It's probably that
B4X:
Dim a As Class1
doesn't actually create the object, it just creates a reference which until (B4X) initialize or (java) new is called, it is null.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
you could change your loop to (using javaobject)
B4X:
    For Each o As Object In a
       
    #if debug
' debug needs 2 parameters
    o.As(JavaObject).RunMethod("_initialize",Array(Null,Null))
    #else
' release only needs 1
    o.As(JavaObject).RunMethod("_initialize",Array(Null))
    #end if   
    Next

My full working code (Class1 is just a bare class)
B4X:
Sub Process_Globals
    Dim a As Class1
    Dim b As Class1
    Dim c() As Object = Array As Object(a,b)
End Sub

Sub AppStart (Args() As String)
    
    Log(a.IsInitialized)
    Log(b.IsInitialized)

    For Each o As Object In c
        #if debug
            o.As(JavaObject).RunMethod("_initialize",Array(Null,Null))
        #else
            o.As(JavaObject).RunMethod("_initialize",Array(Null))
        #end if        
    Next
    
    Log(a.IsInitialized)
    Log(b.IsInitialized)

End Sub
 
Last edited:
Upvote 0
Solution
Top