3

I'm trying to call a Java method from Kotlin but the compiler doesn't manage to find the proper signature.

The method has 2 signatures :

Observable<Query> createQuery(@NonNull final String table, @NonNull String sql, @NonNull String... args) 

Observable<Query> createQuery(@NonNull final Iterable<String> tables, @NonNull String sql, @NonNull String... args)

Then in kotlin I just want to call this, which seems completely valid to me :

db.createQuery("users", "select * from users")

But the compiler say

None of the following functions can be called with the arguments supplied: 
public open fun createQuery(android.support.annotation.NonNull tables: kotlin.(Mutable)Iterable<kotlin.String!>!, android.support.annotation.NonNull sql: kotlin.String!, android.support.annotation.NonNull vararg args: kotlin.String!): rx.Observable<com.squareup.sqlbrite.SqlBrite.Query!>! defined in com.squareup.sqlbrite.BriteDatabase
public open fun createQuery(android.support.annotation.NonNull table: kotlin.String!, android.support.annotation.NonNull sql: kotlin.String!, android.support.annotation.NonNull vararg args: kotlin.String!): rx.Observable<com.squareup.sqlbrite.SqlBrite.Query!>! defined in com.squareup.sqlbrite.BriteDatabase

I read Kotlin and Java method with vararg have some interrop issues, is there a way to fix that without using a java class as a bridge ?

tbruyelle
  • 12,895
  • 9
  • 60
  • 74
  • I was not able to reproduce the issue *(kotlin 0.12.613)* – Lamorak Jul 03 '15 at 11:37
  • If `db` is nullable type (i.e. `DbConnection?`), and the calls cannot be made on a null then in older versions of Kotlin the error message was confusing. It has been improved in later releases of Kotlin. Usually you now get a message about nullability and needing to use safe call, or other null friendly operator. More about null-related operators in: http://stackoverflow.com/questions/34498562/in-kotlin-what-is-the-idiomatic-way-to-deal-with-nullable-values-referencing-o/34498563#34498563 – Jayson Minard Jan 05 '16 at 22:36
  • I'm facing the same issue – Pinaki Acharya Aug 06 '18 at 08:40
  • I have a method in java that accepts a vararg parameter of `int` and I'm trying to invoke that method from a kotlin function that a vararg parameter of `int` but it gives me a compile time error `type mismatch: inferred type is inArray but Int was expected` – Pinaki Acharya Aug 06 '18 at 08:48

1 Answers1

0

It was because the object is of type BriteDatabase?, so I have to use it with db!! or db?.

Maybe the error message should be improved!

tbruyelle
  • 12,895
  • 9
  • 60
  • 74
  • Likely it has been improved in later releases of Kotlin. Usually you now get a message about nullability and needing to use safe call, or other null friendly operator. More about null-related operators in: http://stackoverflow.com/questions/34498562/in-kotlin-what-is-the-idiomatic-way-to-deal-with-nullable-values-referencing-o/34498563#34498563 – Jayson Minard Jan 05 '16 at 22:35