# Initializing struct members after struct has been constructed

**URL:** <https://discourse.julialang.org/t/initializing-struct-members-after-struct-has-been-constructed/30403>\
**Category:** New to Julia\
**Tags:** data\_structures\
**Created:** [October 28, 2019, 2:31pm UTC](https://discourse.julialang.org/t/initializing-struct-members-after-struct-has-been-constructed/30403 "2019-10-28T14:31:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonas\_Hjulstad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonas_hjulstad/32/9146_2.png) [@Jonas\_Hjulstad](https://discourse.julialang.org/u/Jonas_Hjulstad)\
**Post date:** [October 28, 2019, 2:31pm UTC](https://discourse.julialang.org/t/initializing-struct-members-after-struct-has-been-constructed/30403/1 "2019-10-28T14:31:21Z")

</div>

I’m trying to add members to my struct, but get:  
BoundsError: attempt to access 0-element Array{Node,1} at index [1]

```julia
mutable struct Node
    i::Int
    j::Int
    floor::Bool
    neighbors::Array{Node}
    color::Union{String,Nothing}
    distance::Union{Int,Nothing}
    predecessor::Union{Node,Nothing}
end
Node(i, j, floor=true) = Node(i, j, floor, [], nothing, nothing, nothing)

```

A matrix called nodearray is full of initialized nodes, which currently has member neighbors initialized to ‘nothing’

```julia
nodearray[i,j].neighbors[n] = Node(i-1,j)

```

I am not able to add nodes as neighbors. Why?

---

<div class="post-metadata">

**Author:** ![Karajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karajan/32/8545_2.png) [@Karajan](https://discourse.julialang.org/u/Karajan)\
**Post date:** [October 28, 2019, 2:44pm UTC](https://discourse.julialang.org/t/initializing-struct-members-after-struct-has-been-constructed/30403/2 "2019-10-28T14:44:08Z")

</div>

Well, the problem is pretty much exactly as the error message says.  
You have an `0-element Array{Node,1}` (which comes from here: `Node(..., --> [], ...)`) and you want to put something `at index [1]`, but that index doesn’t exist.

But you can still add something to the array: `push!(nodearray[i,j].neighbors[n], Node(i-1,j))`. The other option would be to initialize with an array which has enough space for your needs: `Node(..., Vector{Node}(undef, 10), ...)`.

---

<div class="post-metadata">

**Author:** ![Jonas\_Hjulstad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonas_hjulstad/32/9146_2.png) [@Jonas\_Hjulstad](https://discourse.julialang.org/u/Jonas_Hjulstad)\
**Post date:** [October 28, 2019, 2:50pm UTC](https://discourse.julialang.org/t/initializing-struct-members-after-struct-has-been-constructed/30403/3 "2019-10-28T14:50:59Z")

</div>

push!() could be useful, I resolved it by initializing a fixed array size for neighbors in the construction. Thanks!

---

<div class="post-metadata">

**Author:** ![Karajan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karajan/32/8545_2.png) [@Karajan](https://discourse.julialang.org/u/Karajan)\
**Post date:** [October 28, 2019, 4:29pm UTC](https://discourse.julialang.org/t/initializing-struct-members-after-struct-has-been-constructed/30403/4 "2019-10-28T16:29:27Z")

</div>

You’re welcome!
