Beginner at Kotlin. Need help using Gradle.

Hi everyone. Complete beginner to Kotlin here.

I made a simple calculator app just to learn the basic syntax.

fun main(){ var num1 = 0 var num2 = 0 var oper = 0 println("Please enter your first number...") num1 = readLine()!!.toInt() println("Please enter your second number...") num2 = readLine()!!.toInt() println("Choose an Operator...") println("1 = Add, 2 = Sub, 3 = Mult, 4 = Div") oper = readLine()!!.toInt() when (oper) { 1 -> { println(num1 + num2) } 2 -> { println(num1 - num2) } 3 -> { println(num1 * num2) } 4 -> { println(num1 / num2) } } } 

From here I tried to get it built in gradle. So I did.

gradle init --type kotlin-application 

And then replaced the main function of the auto generated App,kt file like so…

/* * This Kotlin source file was generated by the Gradle 'init' task. */ package calc class App { val greeting: String get() { return "Hello World!" } } fun main() { var num1 = 0 var num2 = 0 var oper = 0 println("Please enter your first number...") num1 = readLine()!!.toInt() println("Please enter your second number...") num2 = readLine()!!.toInt() println("Choose an Operator...") println("1 = Add, 2 = Sub, 3 = Mult, 4 = Div") oper = readLine()!!.toInt() when (oper) { 1 -> { println(num1 + num2) } 2 -> { println(num1 - num2) } 3 -> { println(num1 * num2) } 4 -> { println(num1 / num2) } } } 

This still builds fine. But when I try to “gradle run” it I am given an error.

Exception in thread "main" java.lang.NullPointerException at calc.AppKt.main(App.kt:19) at calc.AppKt.main(App.kt) 

I must confess I have no idea what I am doing, and have no idea why I am now getting a null pointer exception when I never got one when building without Gradle. Any help would be greatly appreciated thanks.

submitted by /u/TheDevilsController
[link] [comments]