B4J Question Issue with Java 11

MrKim

Well-Known Member
Licensed User
Longtime User
Edit: The solution that worked for me is in a post below below but I also want to apologize, the following line of code was somehow omitted from the MeasureText function below.
TB.InitializeStatic("javafx.scene.text.TextBuilder")

I just updated from 8 to 11 and am getting the following error:

B4X:
Waiting for debugger to connect...
Program started.
Unsched Ops: 0.271 Seconds  257 Records
Error occurred on line: 17
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 anywheresoftware.b4j.object.JavaObject.RunMethodJO(JavaObject.java:139)
    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.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 jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    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)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:21)
    at anywheresoftware.b4a.keywords.Common$2$1.run(Common.java:1018)
    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)
On this line: Bounds = TB.RunMethodJO("create",Null).RunMethodJO("text",Array(Text)).RunMethodJO("font",Array(TFont)).RunMethodJO("build",Null).RunMethodJO("getLayoutBounds",Null)
In this module:
B4X:
Sub MeasureText(Text As String,TFont As Font) As TextMetric
    Dim TB, Bounds As JavaObject
    Dim TM As TextMetric
     Bounds = TB.RunMethodJO("create",Null).RunMethodJO("text",Array(Text)).RunMethodJO("font",Array(TFont)).RunMethodJO("build",Null).RunMethodJO("getLayoutBounds",Null)

    TM.Width = Bounds.RunMethod("getWidth",Null)
    TM.Height = Bounds.RunMethod("getHeight",Null)
    Return TM
End Sub
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
TB doesn't appear to be set to anything, maybe you missed a line.

TB.RunMethodJO(... is meaningless unless TB is set to an object or class.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
SOLUTION:

LucaMs Link
https://www.b4x.com/android/forum/threads/measure-text.45750/
To stevel05 post works well and is a drop in replacement.

B4X:
Sub MeasureText(Text As String,TFont As Font) As TextMetric
    Dim TM As TextMetric
    Dim Helper As JavaObject
    Helper.InitializeNewInstance("javafx.scene.text.Text", Array(Text))
    Helper.RunMethod("setFont",Array(TFont))
    Helper.RunMethod("setLineSpacing",Array(0.0))
    Helper.RunMethod("setWrappingWidth",Array(0.0))
    Dim Bounds As JavaObject = Helper.RunMethod("getLayoutBounds",Null)
    TM.Width = Bounds.RunMethod("getWidth",Null)
    TM.Height = Bounds.RunMethod("getHeight",Null)
    Return TM
End Sub

The code above works - I found the final solution he posted failed unless I remmed the line for prefheight so it only returned the width, and the speed was about the same as the one above.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…