As I was playing with the code posted by Erel in this tutorial: Inline Java Code I added a second Initialize method Initialize2.
If you call Initialize2, without first calling Initialize, the Me object is NULL and therefore the assignment nativeMe = Me will raise an exception:
How can I retrieve the context from Initialize2?
B4X:
'Class module Class1
Sub Class_Globals
Private nativeMe As JavaObject
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
nativeMe = Me
Log(nativeMe.RunMethod("Multiply", Array (10)))
End Sub
Public Sub Initialize2
nativeMe = Me
Log(nativeMe.RunMethod("Multiply", Array (10)))
End Sub
#If JAVA
private int mm = 100;
public int Multiply(int i) {
return i * 2 + mm;
}
#End If
If you call Initialize2, without first calling Initialize, the Me object is NULL and therefore the assignment nativeMe = Me will raise an exception:
B4X:
Error occurred on line: 18 (Class1)
java.lang.NullPointerException: null receiver
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:712)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:340)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:244)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:132)
at b4a.example.main.afterFirstLayout(main.java:103)
at b4a.example.main.access$000(main.java:18)
at b4a.example.main$WaitForLayout.run(main.java:81)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
How can I retrieve the context from Initialize2?
Last edited: