Mparame
Mparame

Reputation: 47

Jpype No matching overloads while passing Py Dict to a Map Obj in Java

I am using Jpype to interface between Python and java

While passing a Py Dict to a corresponding Java Map (java.util.Map) Object, I am getting the error as : No matching overloads found. at native\common\jp_method.cpp:121

Here is the sample code

..........python code ....................

1.stuff = {'name': 'Zed'}

2.Obj.testMethodofJava(stuff);#where obj is the object of the Java class

..........python code ....................

*********java code**************************

public void testMethodofJava(HashMap userContextMap)

*********java code**************************

Please note that I am able to access all other member functions of the java class which accept primitive data types like int and string

I tried replacing the HashMap contents with Object (Hashmap), But this also resulted in thee same error

Please help me out in this case.

Thanks in advace

Upvotes: 2

Views: 2040

Answers (2)

Karl Nelson
Karl Nelson

Reputation: 356

JPype only converts to collection interfaces by default. There is no guarantee it will implement any particular concrete class conversion especially for derived classes like HashMap.

Therefore, you will need to insert a constructor for the concrete type you wish to convert to.

    import java
    stuff = {'name': 'Zed'}
    Obj.testMethodofJava(java.util.HashMap(stuff))

I would recommend against the solution that gives map = JObject(stuff, JClass('java.util.Map')) which was depending on the implementation detail that implicit conversion of dict to a Map happened to produce a HashMap. The fact that it worked is a bug because the cast converter should have returned the requested type rather than a derived type. That bug has been corrected for a while.

Generally speaking it is poor design to force a specific concrete type when an interface will do, but of course often the user can't influence the design choices of a library so this often comes up.

Upvotes: 1

Jojo
Jojo

Reputation: 99

I am a fresh hand, and just found this:

    stuff = {'name': 'Zed'}
    map = JObject(stuff , JClass('java.util.Map'))
    Obj.testMethodofJava(map)

Upvotes: 2

Related Questions