B4J Question java.lang.RuntimeException: Method: checkPoints not matched.

Johan Schoeman

Expert
Licensed User
Longtime User
A little bit lost here. Just having one of those stupid moments again and cannot figure out why I am getting "java.lang.RuntimeException: Method: checkPoints not matched."

In the B4J code and inline Java code it logs:

B4X:
b4j.example.main$Point@1625e379
b4j.example.main$Point@50825d5a
b4j.example.main$Point@13b6edf9
b4j.example.main$Point@50262488
b4j.example.main$Point@69a13bde
b4j.example.main$Point@2b0bc33d
b4j.example.main$Point@ff63a80
***************
(Point) b4j.example.main$Point@1625e379
(Point) b4j.example.main$Point@50825d5a
(Point) b4j.example.main$Point@13b6edf9
(Point) b4j.example.main$Point@50262488
(Point) b4j.example.main$Point@69a13bde
(Point) b4j.example.main$Point@2b0bc33d
(Point) b4j.example.main$Point@ff63a80
main._appstart (java line: 130)
java.lang.RuntimeException: Method: checkPoints not matched.

So, it seems that the instances in the B4J and inline Java code are the same but for some reason it says the methods are not matched when calling the inline Java code from the B4J code.

Can someone please guide this dummy (me.As(Stupid)) in the right direction?

JS
 

Attachments

  • b4jTest.zip
    1.9 KB · Views: 27
Solution
B4X:
nativeMe.RunMethod("checkPoints", Array(aa))
'equivalent to:
nativeMe.RunMethod("checkPoints", Array As Object(aa))

This code will work:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    nativeMe = Me
    Dim aa(7) As Object
    Dim x As Int = 0
    Dim y As Int = 3
    aa(0) = CreatePoint(x, y)
    Dim x As Int = 2
    Dim y As Int = 3
    aa(1) = CreatePoint(x, y)
    Dim x As Int = 1
    Dim y As Int = 1
    aa(2) = CreatePoint(x, y)
    Dim x As Int = 2
    Dim y As Int = 1
    aa(3) = CreatePoint(x, y)
    Dim x As Int = 3
    Dim y As Int = 0
    aa(4) = CreatePoint(x, y)
    Dim x As Int = 0
    Dim y As Int = 0
    aa(5) =...

Daestrum

Expert
Licensed User
Longtime User
Just change
B4X:
public static void checkPoints (Point points[]) {

to
B4X:
public static void checkPoints (Object points[]) {
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Just change
B4X:
public static void checkPoints (Point points[]) {

to
B4X:
public static void checkPoints (Object points[]) {
Mind boggling - would never have gotten there. There seems to be a "casting" between B4J and inline Java with (Point)...... but still dont understand why Object fixes the issue. Can you shed some light on this?
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Not sure, maybe it was confusing it with awt Point - will have a play and see if I find the true cause.
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Not sure, maybe it was confusing it with awt Point - will have a play and see if I find the true cause.
Can you try and run this B4J project - sure you will figure out the code/logic. It is OK when calling checkPoints but crashes when calling main1...

B4X:
'    nativeMe.RunMethod("checkPoints", Array(aa))  ----> Working
    nativeMe.RunMethod("main1", Array(aa, leng))  ----> Crashes
 

Attachments

  • b4jConcaveHull.zip
    3.4 KB · Views: 20
Upvote 0

agraham

Expert
Licensed User
Longtime User
but still dont understand why Object fixes the issue.
Array aa() is an array of JavaObjects containing instances of Points whereas checkPoints() is expecting an array of Points. Everything in Java inherits from Object so anything can be successfully cast to an Object.

Can you try and run this B4J project
It doesn't compile for me - giving this error

src\b4j\example\mygfg.java:274: error: incompatible types: Object[] cannot be converted to Point[]

This because of the reverse of the above. Point inherits from Object so can be cast to an Object. Object does not inherit from Point so you can't cast it to one.

Why don't you just write it in B4J, why do you need Java code?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
nativeMe.RunMethod("checkPoints", Array(aa))
'equivalent to:
nativeMe.RunMethod("checkPoints", Array As Object(aa))

This code will work:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    nativeMe = Me
    Dim aa(7) As Object
    Dim x As Int = 0
    Dim y As Int = 3
    aa(0) = CreatePoint(x, y)
    Dim x As Int = 2
    Dim y As Int = 3
    aa(1) = CreatePoint(x, y)
    Dim x As Int = 1
    Dim y As Int = 1
    aa(2) = CreatePoint(x, y)
    Dim x As Int = 2
    Dim y As Int = 1
    aa(3) = CreatePoint(x, y)
    Dim x As Int = 3
    Dim y As Int = 0
    aa(4) = CreatePoint(x, y)
    Dim x As Int = 0
    Dim y As Int = 0
    aa(5) = CreatePoint(x, y)
    Dim x As Int = 3
    Dim y As Int = 3
    aa(6) = CreatePoint(x, y)
    Dim leng As Int = aa.Length
    Log("***************")
    For i = 0 To leng - 1
        Log(aa(i))
    Next
    Dim arr As JavaObject
    arr.InitializeArray("b4j.example.main.Point", aa)
    nativeMe.RunMethod("checkPoints", Array(arr))
End Sub

Private Sub CreatePoint(x As Int, y As Int) As Object
    Dim p As JavaObject
    p.InitializeNewInstance("b4j.example.main.Point", Array(x, y))
    Return p
End Sub

#If Java

public static class Point {
    public int x, y;
    public Point(int x, int y){
        this.x=x;
        this.y=y;
        BA.Log("" + this);
    }
}

public static void checkPoints (Point points[]) {
    BA.Log("" + points);
}

#End If
 
Upvote 0
Solution

Johan Schoeman

Expert
Licensed User
Longtime User
BTW, I was surprised that the Java code compiled successfully:
B4X:
public static void checkPoints (Point points[])

The square brackets usually go with the type, not the variable (contrary to B4X and VB).
And so we learn every day. Did not know that to be the case Erel.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Upvote 0
Top