Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lateinit modifier is not allowed on primitive type properties in Kotlin

Tags:

android

kotlin

I am defining like a instance variable in kotlin and want to initialize it onCreate method of an activity.

var count: Int override fun onCreate(savedInstanceState: Bundle?) {     super.onCreate(savedInstanceState)     setContentView(R.layout.activity_main)     count.inc() } 

Here I am getting a below error on count variable.

Property must be initialized or be abstract in Kotlin

Well, I read this thread Property must be initialized or be abstract and tried same but again I am getting a below error.

lateinit modifier is not allowed on primitive type properties

lateinit var count: Int override fun onCreate(savedInstanceState: Bundle?) {     super.onCreate(savedInstanceState)     setContentView(R.layout.activity_main)     count.inc() } 

Is there any way to do this in Kotlin ?

like image 605
N Sharma Avatar asked Jun 06 '17 05:06

N Sharma


People also ask

What is Lateinit modifier in Kotlin?

The Kotlin lateinit modifier is used when you want to declare a non-nullable variable without initializing the value. To use the modifier, you need to add it before the variable name as follows: lateinit var message: String. A variable declared with lateinit must use the var keyword instead of val .

What is Lateinit in Kotlin?

The lateinit keyword allows you to avoid initializing a property when an object is constructed. If your property is referenced before being initialized, Kotlin throws an UninitializedPropertyAccessException , so be sure to initialize your property as soon as possible.

Does Kotlin support primitive data types?

Kotlin doesn't have primitive type (I mean you cannot declare primitive directly). It uses classes like Int , Float as an object wrapper for primitives. When kotlin code is converted to jvm code, whenever possible, "primitive object" is converted to java primitive.

How do I declare a Lateinit variable in Kotlin?

NOTE: To use a lateinit variable, your variable should use var and NOT val . Lateinit is allowed for non-primitive data types only and the variable can't be of null type. Also, lateinit variable can be declared either inside the class or it can be a top-level property.


1 Answers

There are several ways to resolve this issue.

You can Initialise it with default value (e.i 0 or -1 or whatever) and then initialise it whenever your logic says.

Or tell compiler that count will be initialised later in this code by using Delegates.notNull check notNull.

var count: Int by Delegates.notNull<Int>()  override fun onCreate(savedInstanceState: Bundle?) {     super.onCreate(savedInstanceState)     setContentView(R.layout.activity_main)      // You can not call `Int.inc()` in onCreate()` function until `count` is initialised.     // count.inc()     // **initialise count**  } 

And if you need count value on demand (if not necessary to initialise in onCreate), you can use lazy function. Use this only if you have an intensive (Some calculation/Inflating a layout etc) task that you want to do on demand, Not to just assign a value.

var count:Int by lazy {     // initialise } 

Now you can decide what to use.

I hope it helps.

like image 79
chandil03 Avatar answered Sep 22 '22 14:09

chandil03