Kotlin array initialization throws null pointer exception
up vote
1
down vote
favorite
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.
kotlin
|
show 2 more comments
up vote
1
down vote
favorite
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.
kotlin
8
Code works fine as is and prints out4
.
– Robby Cornelissen
Oct 23 at 2:27
The code has no problem. How about uploading the IDE screenshot with NPE.
– Naetmul
Oct 23 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.
– tynn
Oct 23 at 6:41
1
I tried that, got the exact same error
– John H
Oct 24 at 7:20
1
Same problem as stackoverflow.com/questions/53233325/…
– yole
Nov 17 at 17:39
|
show 2 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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.
kotlin
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.
kotlin
kotlin
edited Oct 23 at 18:30
Tom
29627
29627
asked Oct 23 at 2:23
John H
62
62
8
Code works fine as is and prints out4
.
– Robby Cornelissen
Oct 23 at 2:27
The code has no problem. How about uploading the IDE screenshot with NPE.
– Naetmul
Oct 23 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.
– tynn
Oct 23 at 6:41
1
I tried that, got the exact same error
– John H
Oct 24 at 7:20
1
Same problem as stackoverflow.com/questions/53233325/…
– yole
Nov 17 at 17:39
|
show 2 more comments
8
Code works fine as is and prints out4
.
– Robby Cornelissen
Oct 23 at 2:27
The code has no problem. How about uploading the IDE screenshot with NPE.
– Naetmul
Oct 23 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.
– tynn
Oct 23 at 6:41
1
I tried that, got the exact same error
– John H
Oct 24 at 7:20
1
Same problem as stackoverflow.com/questions/53233325/…
– yole
Nov 17 at 17:39
8
8
Code works fine as is and prints out
4
.– Robby Cornelissen
Oct 23 at 2:27
Code works fine as is and prints out
4
.– Robby Cornelissen
Oct 23 at 2:27
The code has no problem. How about uploading the IDE screenshot with NPE.
– Naetmul
Oct 23 at 3:52
The code has no problem. How about uploading the IDE screenshot with NPE.
– Naetmul
Oct 23 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.
– tynn
Oct 23 at 6:41
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.
– tynn
Oct 23 at 6:41
1
1
I tried that, got the exact same error
– John H
Oct 24 at 7:20
I tried that, got the exact same error
– John H
Oct 24 at 7:20
1
1
Same problem as stackoverflow.com/questions/53233325/…
– yole
Nov 17 at 17:39
Same problem as stackoverflow.com/questions/53233325/…
– yole
Nov 17 at 17:39
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
5
down vote
This is a bug in the AOT-compiled (OS-dependent) version of the Kotlin compiler. Please see the issue for updates.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
This is a bug in the AOT-compiled (OS-dependent) version of the Kotlin compiler. Please see the issue for updates.
add a comment |
up vote
5
down vote
This is a bug in the AOT-compiled (OS-dependent) version of the Kotlin compiler. Please see the issue for updates.
add a comment |
up vote
5
down vote
up vote
5
down vote
This is a bug in the AOT-compiled (OS-dependent) version of the Kotlin compiler. Please see the issue for updates.
This is a bug in the AOT-compiled (OS-dependent) version of the Kotlin compiler. Please see the issue for updates.
answered Nov 19 at 10:03
yole
57.1k11139138
57.1k11139138
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52940272%2fkotlin-array-initialization-throws-null-pointer-exception%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
8
Code works fine as is and prints out
4
.– Robby Cornelissen
Oct 23 at 2:27
The code has no problem. How about uploading the IDE screenshot with NPE.
– Naetmul
Oct 23 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.
– tynn
Oct 23 at 6:41
1
I tried that, got the exact same error
– John H
Oct 24 at 7:20
1
Same problem as stackoverflow.com/questions/53233325/…
– yole
Nov 17 at 17:39