why do i get `> java.lang.NullPointerException ...
# getting-started
s
why do i get
> java.lang.NullPointerException (no error message)
for
v[0]!!::class.objectInstance
s
Read the docs for
objectInstance
and it'll tell you why
s
ok
The instance of the object declaration, or null if this class is not an object declaration.
s
yep. is
v[0]
an
object
, or is it an instance of a class?
s
im passing it
f
which is val
f = arrayListOf<a>()
which is
Copy code
private class a {
    var empty : Int = 0
}
s
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
v[0] is an instance of a class
s
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
why would it be null?
s
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
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
You won't get back an
E
, you'll get back a
Class<E>
.