https://kotlinlang.org logo
Title
s

Smallville7123

04/09/2019, 1:03 AM
why do i get
> java.lang.NullPointerException (no error message)
for
v[0]!!::class.objectInstance
s

serebit

04/09/2019, 1:05 AM
Read the docs for
objectInstance
and it'll tell you why
s

Smallville7123

04/09/2019, 1:06 AM
ok
The instance of the object declaration, or null if this class is not an object declaration.
s

serebit

04/09/2019, 1:07 AM
yep. is
v[0]
an
object
, or is it an instance of a class?
s

Smallville7123

04/09/2019, 1:08 AM
im passing it
f
which is val
f = arrayListOf<a>()
which is
private class a {
    var empty : Int = 0
}
s

serebit

04/09/2019, 1:08 AM
that... doesn't answer my question
if you're just starting out with kotlin, you should try the koans to get a handle on things before jumping in and trying random things like this
s

Smallville7123

04/09/2019, 1:11 AM
v[0] is an instance of a class
s

serebit

04/09/2019, 1:12 AM
so then
objectInstance
is going to be null, and if you don't handle that before passing it to a Java function, it might throw a NullPointerException.
s

Smallville7123

04/09/2019, 1:12 AM
why would it be null?
s

serebit

04/09/2019, 1:12 AM
The instance of the object declaration, or null if this class is not an object declaration.
it says it right there. if it's an
object
, it'll return the object, if it's a
class
, it'll return null.
s

Smallville7123

04/09/2019, 1:14 AM
ok, so how do i return the class itself?
for example,
v.add(a() as E)
doing
v.add(v[0]!!.javaClass as E)
gives me
java.lang.Class cannot be cast to preprocessor.utils.core.a
k

karelpeeters

04/09/2019, 6:57 AM
You won't get back an
E
, you'll get back a
Class<E>
.