1

I'm a total beginner with kotlin, and I'm trying to initialize an array using the constructor that accepts a size and a function. This is what I have:

fun main(args: Array<String>) {
    var ary = Array(5, {x -> x + 2})
    println(ary[2])
}

But it's throwing a null pointer exception on the first line. Why is this happening?


EDIT: I'm the original poster's professor, and we're trying to get this working together. Here's some more info:

I've edit the code above to be the full program. The version of Kotlin we have installed on our server is:

$ kotlinc -version
info: kotlinc-jvm 1.2.70 (JRE 1.8.0_144-jdk_2017_08_24_20_46-b00)

When I try to compile and run from the command line I get:

$ kotlinc example.kt -include-runtime -d example.jar
$ java -jar example.jar 
Exception in thread "main" java.lang.NullPointerException
    at ExampleKt.main(example.kt:2)

I'll note that this code compiles just fine for me on my laptop, which has an older version of Kotlin.

7
  • 8
    Code works fine as is and prints out 4. Commented Oct 23, 2018 at 2:27
  • The code has no problem. How about uploading the IDE screenshot with NPE. Commented Oct 23, 2018 at 3:52
  • If you have an exception it might be worth adding the stacktrace to the question as well. Usually this gives you more information about the why the error happend. Commented Oct 23, 2018 at 6:41
  • 1
    I tried that, got the exact same error Commented Oct 24, 2018 at 7:20
  • 1
    Same problem as stackoverflow.com/questions/53233325/… Commented Nov 17, 2018 at 17:39

1 Answer 1

5

This is a bug in the AOT-compiled (OS-dependent) version of the Kotlin compiler. Please see the issue for updates.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.