1

Please, just keep in mind that I'm ramping up on functional programming hehe.

I have defined a Mutable list like this:

 var list: MutableList<E>? = null

So, when I try to use list!!.add(E()) this throws a

kotlin.KotlinNullPointerException.

I understand that this is because I assign null to this list when I define it but a didn't get with a right solution thinking about on functional programming aspects how to solve this. Can you suggest me some code or concepts to achieve this situation.

Ilya
  • 21,871
  • 8
  • 73
  • 92
Jimmy Alvarez
  • 587
  • 1
  • 5
  • 13
  • Are you asking how to store something other than `null`? How to access a nullable object without using `!!`? Both of those are covered in the language docs. – chris Oct 15 '17 at 18:34
  • I want to store a new item on the list but this throws the exception when I try to use the method .add which is what I use to store items in java. – Jimmy Alvarez Oct 15 '17 at 18:43
  • But you don't have a list in the first place, you have `null`. The main change from Java there is simply that Kotlin gives you the option of non-nullable references, and goes as far as making that the default. You have to opt into `null`, and unchecked access won't compile (you told the compiler with `!!` that you guarantee it isn't `null`, which was a lie), so it's considerably harder to accidentally have a `NullPointerException`. – chris Oct 15 '17 at 18:52
  • Yes, that is what is happening, what can I do to solve this kind of problems. In may case I do't know long this list will be so I can put a fix size. in nutshell I can initialize it before or a don't know how to do that. – Jimmy Alvarez Oct 15 '17 at 19:09
  • You likely don't even need it to be nullable in the first place, but either way, [create an object](https://kotlinlang.org/docs/reference/classes.html#creating-instances-of-classes) instead of using `null`, just like you would in Java. – chris Oct 15 '17 at 19:12
  • It isn't `add` which throws the exception, it's `!!`. – Alexey Romanov Oct 16 '17 at 08:06

2 Answers2

2

You need to initialize the variable with an instance of MutableList before calling any methods on it.

list = mutableListOf<E>() // creates an empty mutable list
list!!.add(E())

If you initialize variable right at the declaration, you even don't need to declare it as nullable and var.

val list = mutableListOf<E>()
...
list.add(E())
Ilya
  • 21,871
  • 8
  • 73
  • 92
  • Thank you so much, this works perfect. I didn't know there are methods like that to initialize objects. – Jimmy Alvarez Oct 15 '17 at 20:46
  • You should obtain an instance of your class in some way, for example by calling its constructor: `val person = Person(name, age)` – Ilya Oct 15 '17 at 20:46
  • I get it, Thanks for your help. I am enjoining this awesome concepts included in Functional Programming. – Jimmy Alvarez Oct 15 '17 at 20:48
  • 1
    Actually this isn't related at all to functional programming, it's more likely about object oriented programming. – Ilya Oct 15 '17 at 21:03
0

Here is the proper declaration List.

private var mList = mutableListOf<String>()//You can change the String class to your data model

You can add values like this:

mList.add(String)//you can assign your data model class
Kashif Ahmad
  • 203
  • 3
  • 15