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.










share|improve this question



















  • 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














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.










share|improve this question



















  • 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












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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












  • 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







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












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.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer
























      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.






      share|improve this answer






















        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.






        share|improve this answer












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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 10:03









        yole

        57.1k11139138




        57.1k11139138



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo