You can also check if it's null when calling the m...
# getting-started
k
You can also check if it's null when calling the method.
d
it's an information-dump page where i just list whatever information that is available for an object
Copy code
$msg.get("PAYMENT_DATE", $transaction.paidDate)
$msg.get("PAYMENT_METHOD", $transaction.paidWith)
$msg.get("PAYMENT_AMOUNT", $transaction.totalAmountPreVat, $transaction.currency)
i don't really care if anything is null
i'm not really looking for alternate solutions, the approach i have currently works well for java
i was just wondering if there was a nicer way of writing the function
k
Then what did you mean with "varargs can't be null"? They certainly can, if you give them a nullable type like in @menegatti's example.
d
then you'll have to pass an array of nulls, you can't pass just a singular null value
k
Can you give some example of how you want this to work?
Copy code
get("key", "a", "b", "c")
get("key", "a", null)
get("key", null, "a")
get("key", null)
get("key")
get("key", *arrayOf("a", null))
get("key", *arrayOf<String>())
All of those would work with the vararg one.
d
hmm,
get("key", null)
won't work for me
Copy code
fun get(key: String, vararg args: Any?): String {
    return MessageFormat.format(get(key), *args)
}
Parameter specified as non-null is null: method util.MessageBundle.get, parameter args
😵 1
k
Huh that is strange, I wonder if it's intended.
d
i made a new project and just ran your snippet, there everything works 🤔
k
Do both projects have different compiler versions?
d
get
is being called from apache velocity in my original project
not sure why that would matter though
k
Did you actually run the broken one or is that just Idea complaining?
d
i ran it
k
Compiler versions?
d
Copy code
org.apache.velocity.exception.MethodInvocationException: Invocation of method 'get' in  class util.MessageBundle threw exception java.lang.IllegalArgumentException: Parameter specified as non-null is null: method util.MessageBundle.get, parameter args at /velocity/views/data-dump.vm[line 5, column 41]
Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method util.MessageBundle.get, parameter args
k
Right that's just Kotlin's build-in runtime null checking complaining.
d
why would it complain when calling the function from velocity, but not from a
fun main
?
Copy code
kotlin 1.1.2-2 
target jvm1.8 
javac 1.8.0_101
b
Oh yeah in Java, there's no obvious way to distinguish
get("key", {1, 2, 3})
from
get("key", 1, 2, 3)
, so in Java,
get("key", null)
is like
get("key", *null)
in Kotlin. Assuming the calling code is Java
k
Ah that could be the difference, is the calling code Java?
d
@benleggiero thanks, does that mean i'm stuck with this?
Copy code
fun get(key: String, args: Array<Any?>?): String {
    if (args == null) return MessageFormat.format(get(key), args)
    return MessageFormat.format(get(key), *args)
}
@karelpeeters yes, sorry if i failed to make that clear
the function is being called from apache-velocity, a java-based template engine
k
You can overload it with a version that takes a single extra parameter and calls the other one.
âž• 1
d
🤔