``` class A { val foo = "hello" } class B(a: A...
# announcements
i
Copy code
class A {
    val foo = "hello"
}
class B(a: A) {
    init {
        a.foo.bar // null pointer
    }
}
`
s
bar
? Where does
bar
come from?
i
my
foo
isn't a string. It's just a method of the field
point being
a.foo
is
null
s
Could you post a snippet of the actual code so that we can see what might wrong?
E.g. also how the constructor of the
B
class is called.
i
hm yeah I can't reproduce it with this snippet. Will play a bit with a scratch and post if I can't figure it out
ok, so the problem is this
Copy code
class B(a: A) {
    init {
        println(a.foo.length)
    }
}

class A {
    val b = B(this)
    val foo = "hello"
}

B(A())
And the solution is to move the line with
val foo = "hello"
above the one that's initializing
B
m
circular dependencies are a b**ch
s
yes, this makes sense.
foo
has not yet been initialized when you access it in `B`’s constructor block
r
Nothing unexpected here
s
Try something like this. It won’t run:
Copy code
open class A(val b: B)
open class B(val a: A)

object objectA : A(objectB)
object objectB : B(objectA)
If you change it to this:
Copy code
open class A(val b: B?)
open class B(val a: A?)
Then the value of
objectA.b
or
objectB.a
may or may not be null… 🙂
i
yeah I understand the reason... bit hairy 😄
thanks for the help in any case!
s
you probably did this already, if not, just exchange the lines in A
Copy code
class B(a: A) {
    init {
        println(a.foo.length)
    }
}

class A {
    val foo = "hello"
    val b = B(this)
}

B(A())
i
@Stephan Schroeder yes I did 😄