B4J Question jGauges javaobject inline casting not working

Chris2

Active Member
Licensed User
I'm getting an error when using inline casting to set the parameter type for a javaobject call to jGauges.

For example, "setMinValue" & "setMaxValue" want a double. But adding to the jGauges example app:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    :
    :
    :
    SetMaxMin
    
End Sub


Private Sub SetMaxMin

    Dim joGauge As JavaObject = Gauge1
    
    Dim minVal As Float = 0
    Dim maxVal As Float = 100

    joGauge.RunMethod("setMinValue", Array(minVal.As(Double)))
    joGauge.RunMethod("setMaxValue", Array(maxVal.As(Double)))
    
End Sub

Generates the error:
B4X:
java.lang.RuntimeException: Method: setMinValue not matched.
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:130)
    at b4j.example.main._setmaxmin(main.java:202)
    at b4j.example.main._appstart(main.java:128)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at b4j.example.main.start(main.java:38)
    at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at [email protected]/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at [email protected]/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at [email protected]/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:833)

But using the following instead works successfully:
B4X:
Private Sub SetMaxMin
    Dim joGauge As JavaObject = Gauge1
    
    Dim minVal As Float = 0
    Dim maxVal As Float = 100
    
    Dim minDouble As Double = minVal
    Dim maxDouble As Double = maxVal
    joGauge.RunMethod("setMinValue", Array(minDouble))
    joGauge.RunMethod("setMaxValue", Array(maxDouble))
    
End Sub

Am I doing something incorrectly with the inline casting?
 

DonManfred

Expert
Licensed User
Longtime User
Gauge1 is a wrapperobject above the Gauge itself.

So you probably have to get the native Gauge from it first.

B4X:
joGauge.RunMethod("getObject", Null)

it returns another JavaObject. Hopefully the Gauge.
Call setMinValue on this now.
 
Upvote 0

Chris2

Active Member
Licensed User
Thanks for the help, but
joGauge.RunMethod("getObject", Null)
errors with:
B4X:
java.lang.RuntimeException: Method: getObject not found in: eu.hansolo.medusa.Gauge

Also, if I wasn't already getting to the gauge itself then casting the variables to Doubles before doing the joGauge.RunMethod wouldn't work would it?
As in the second set of code in my first post, which already works OK...
B4X:
Dim minDouble As Double = minVal
    Dim maxDouble As Double = maxVal
    joGauge.RunMethod("setMinValue", Array(minDouble))
    joGauge.RunMethod("setMaxValue", Array(maxDouble))
 
Upvote 0

Chris2

Active Member
Licensed User
So the call to setMinValue is not correct it seems.
But it works OK if I cast the value to a double before using it:
B4X:
Dim minVal As Float = 0

'works
Dim minDouble As Double = minVal
joGauge.RunMethod("setMinValue", Array(minDouble))

'fails, why?
joGauge.RunMethod("setMinValue", Array(minVal.As(Double)))
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
Erel would have to explain why but As() doesn't necessarily always do what you might expect. In this case it is ignored in the compiled java which just casts the float value to Object for inclusion in the parameter array,

Java:
 //BA.debugLineNum = 72;BA.debugLine="joGauge.RunMethod(\"setMinValue\", Array(minVal.As(Double)))";
_jogauge.RunMethod("setMinValue",new Object[]{(Object)((_minval))});
 
Upvote 0
Top