Java Kotlin border crossing checks

If you have a Java project, slowly converting to Kotlin, and have some interoperable code of Java and Kotlin, this would be important checks.

The two items shared below has bitten us, as it won’t be caught by Compiler. Hopes you could be cautious of them.

Java calls Kotlin code

You have two piece of codes as below

public class Caller{
public void startHere() {
Receiver receiver = new Receiver();
receiver.printString(null);
}
}

And

public class Receiver {
public void printString(String message) {
System.out.println(message);
}
}

Then you decide to convert the Receiver to Kotlin, but not the Caller for some reason. Using Command-Option-Shift-K shortcut key, your Receiver now is as below

class Receiver {
fun printString(message: String) {
println(notNullString)
}
}

You compile the code, all good, no error.

You run the code … CRASH!!

java.lang.IllegalArgumentException: Parameter specified as non-null is null

What do to?

Check all the function caller from Java to your newly introduce Kotlin function, to make sure they are sending the equivalent non-nullable parameter.

Kotlin calls Java code

You have two piece of codes as below

public class Caller {
public void startHere() {
Receiver receiver = new Receiver();
List myList =
new ArrayList<>(Arrays.asList("a", "b", "c"));
receiver.appendList(myList, "d");
}
}

And

public class Receiver {
public void appendList(List original, String addition) {
original.add(addition);
}
}

Then you decide to convert the Caller to Kotlin, but not the Receiver for some reason. Using Command-Option-Shift-K shortcut key and some modification, your Caller now is as below

class Caller {
fun startHere() {
val receiver = Receiver()
val value = listOf("a", "b", "c")
receiver.appendList(value, "d")
}
}

You compile the code, all good, no error.

You run the code … CRASH!!

java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)

What do to?

Check all the collections sent through the parameter are in compatible mutable or read-only condition. Kotlin collection be default is read-only, hence can’t have things like add, put etc.

Lesson learnt

Although the above example shows simple codes, seems easily discoverable, they are more subtle in reality, especially for bigger projects, where a function is called by multiple places, or the collection creations is not as straightforward (e.g. use toList()). It could cause real problem if not taken care off.

The above 2 cases might not cover all. Hence my recommendation is for all interfaces between Java and Kotlin, may it through function or class inheritance etc, do check them through to ensure both are consistent.

If you find this post is helpful, do click 👏👏👏 to share with others. Thanks!

You could check out my other interesting topics here.

Follow me on medium, Twitter or Facebook for little tips and learning on Android, Kotlin etc related topics. ~Elye~

Please follow and like us:
0

Gurupriyan is a Software Engineer and a technology enthusiast, he’s been working on the field for the last 6 years. Currently focusing on mobile app development and IoT.