# Which reference is undefined here?

**URL:** <https://discourse.julialang.org/t/which-reference-is-undefined-here/65707>\
**Category:** General Usage\
**Created:** [August 3, 2021, 12:19am UTC](https://discourse.julialang.org/t/which-reference-is-undefined-here/65707 "2021-08-03T00:19:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![trg818](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trg818/32/219149_2.png) [@trg818](https://discourse.julialang.org/u/trg818)\
**Post date:** [August 3, 2021, 12:19am UTC](https://discourse.julialang.org/t/which-reference-is-undefined-here/65707/1 "2021-08-03T00:19:00Z")

</div>

Hi,

I’m still quite new to Julia and am trying to make a simple plot. Please consider the following:

```julia
zth=Vector{Vector{Float64}}(undef,length(DaT))
tth=Vector{Vector{Float64}}(undef,length(DaT))
...
zthcnvplot=plot(title="Position",titlefontsize=10,
        xlabel="t",ylabel="z",legend=:topleft,grid=false)
for (i,D) in enumerate(DaT)
    zthcnvplot=plot!(tth[i],zth[i],label="Dimp="*string(D/1000)*" km")
end
display((zthcnvplot))

```

zth and tth are vectors of vectors, which have correctly been calculated in the part indicated with …, and the lengths match and are preset correctly. Nonetheless, I get an error

```julia
UndefRefError: access to undefined reference
Stacktrace:
 [1] getindex(A::Vector{Vector{Float64}}, i1::Int64)
   @ Base ./array.jl:801
 [2] top-level scope
   @ ./In[79]:31
 [3] eval
   @ ./boot.jl:360 [inlined]
 [4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1094

```

Line 31 is the line in the `for` loop. What is wrong here, which reference is missing? All variables I use have been defined somewhere before.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 3, 2021, 4:42am UTC](https://discourse.julialang.org/t/which-reference-is-undefined-here/65707/2 "2021-08-03T04:42:07Z")

</div>

Have you filled both arrays completely? I.e., are any indices still `undef` from your initialization?

---

<div class="post-metadata">

**Author:** ![trg818](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trg818/32/219149_2.png) [@trg818](https://discourse.julialang.org/u/trg818)\
**Post date:** [August 3, 2021, 10:35am UTC](https://discourse.julialang.org/t/which-reference-is-undefined-here/65707/3 "2021-08-03T10:35:41Z")

</div>

Good point - turns out: no. So, thanks for that.

This of course raises (again) the question to the developers why error messages in Julia are usually rather useless. A much more useful error message would have been “Trying to plot undefined array values in array tth”. An “undefined reference” can really be anything, and the least an error message should do is to tell which of usually several variables in a line causes the trouble. This is never done in Julia, and it needs to be fixed.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [August 3, 2021, 10:56am UTC](https://discourse.julialang.org/t/which-reference-is-undefined-here/65707/4 "2021-08-03T10:56:20Z")

</div>

I don’t know, it looks like the error message was enough for someone on here to figure out the problem without seeing your full code or an MWE?

Also it looks like you’re working in a Jupyter notebook and the stacktrace says

```julia
In[79]:31

```

Is the error not on line 31?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [August 3, 2021, 10:57am UTC](https://discourse.julialang.org/t/which-reference-is-undefined-here/65707/5 "2021-08-03T10:57:14Z")

</div>

I think this was a pretty clear cut case, it tells you that the undefined value was in one of the `getindex` operations on that line, so it’s narrowed down to two variables. It doesn’t say `trying to plot undefined values` because the error happens _before_ the plotting stage, while dereferencing the arguments. If you get an error like this, you can split it up into

```julia
for i in indices
    x = X[i]
    y = Y[i]
    plot(x, y; st=:scatter)
end

```

if you need more detail. And I’m guessing you would get more helpful messages if you avoided indexing in the first place:

```julia
for (t, z, D) in zip(tth, zth, DData)

```

or even better, simply plot the vectors against each other.

---

<div class="post-metadata">

**Author:** ![trg818](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trg818/32/219149_2.png) [@trg818](https://discourse.julialang.org/u/trg818)\
**Post date:** [August 3, 2021, 11:13am UTC](https://discourse.julialang.org/t/which-reference-is-undefined-here/65707/6 "2021-08-03T11:13:59Z")

</div>

Maybe Sukera figured it out because they made the same mistake before and certainly has more experience than I.  
Anyway, yes, I am working in a Jupyter notebook, and the error is in indeed in line 31. However, there are several variables in that line, and the error doesn’t tell which one causes the problem. I see no reason why it doesn’t. For instance, if I compile Fortran code with gfortran and there is an error, I usually get that information, and it speeds up debugging a lot.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 3, 2021, 11:29am UTC](https://discourse.julialang.org/t/which-reference-is-undefined-here/65707/7 "2021-08-03T11:29:29Z")

</div>

Maybe I should clarify how I figured this out in the first place. The information you provided (it’s the line with `plot!`) together with the stacktrace is plenty.

- The stacktrace itself doesn’t contain `plot!`, so it has to happen before `plot!` is called.
- The top frame of the stacktrace contains `getindex(A::Vector{Vector{Float64}}, i1::Int64)`, so it errored trying to access some index of an array.
- The only indexing expressions in the line with `plot!` are `tth` and `zth`, both of which you showed to be initialized with `undef` earlier.

Based on that, the logical conclusion is that trying to access either of the two errored (since `zth` and `tth` itself exist), which led me to ask “are you sure you initialized them properly”.

* * *

I don’t know how Fortran would tell you during runtime which variable contained the error (unless that was somehow baked into each function call or Fortran would know beforehand that it will error), so I’m kind of confused why/how you expect julia to magically know this. Julia only tracks source code line numbers in the compiled code, not columns or individual expressions. Do you have an example from Fortran, so we could maybe improve the error messages?

---

<div class="post-metadata">

**Author:** ![trg818](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trg818/32/219149_2.png) [@trg818](https://discourse.julialang.org/u/trg818)\
**Post date:** [August 3, 2021, 11:38am UTC](https://discourse.julialang.org/t/which-reference-is-undefined-here/65707/8 "2021-08-03T11:38:01Z")

</div>

I just tried your last suggestion and get the same “access to undefined reference”. Moreover, there is only one line in that loop, and there it says that stuff is plotted; it doesn’t say anything about getindex or whatever else happens under the hood, and that is really one of the problems I as a beginner have: \>90% of an error message stacktrace lists some stuff that happens under the hood, often in some library I have never heard of and will never look into, and I have to pick out the little nugget of information that I can actually relate to something I see in my code.

I hope my comments are not taken as bad-mouthing Julia, because that’s not my intention. Quite to the contrary, I try to raise attention to a problem I have had for many months which I think makes getting acquainted with Julia unnecessarily difficult for beginners and people who just want to get stuff done.

---

<div class="post-metadata">

**Author:** ![trg818](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trg818/32/219149_2.png) [@trg818](https://discourse.julialang.org/u/trg818)\
**Post date:** [August 11, 2021, 10:20pm UTC](https://discourse.julialang.org/t/which-reference-is-undefined-here/65707/9 "2021-08-11T22:20:17Z")

</div>

Sorry for taking so long to reply, and thank you for your explanation.  
I guess your second last sentence gets to the point where I have different expectations of what a compiler/interpreter or a running program should report as an error.  
I have tried to cook up a small example of a bug that “works” in both Fortran 90 and Julia, and although it’s less than I had hoped (time is limited), the following might serve to demonstrate at least one issue.  
The following short program with a bug is quite silly and useless but I hope it shows what I mean. If you compile this Fortran version:

```julia
program bd
implicit none
integer, parameter :: n=20
integer :: i,j=100
real, dimension(n) :: x,y
real :: dx=0.1
do i=1,n
   x(i)=i*dx
   y(j)=3*x(j)
end do
end program bd

```

with gfortran (v.11.0 in my case) **using the flag -fbounds-check** , you get this error:

```julia
At line 9 of file bugdemo.f90
Fortran runtime error: Index '100' of dimension 1 of array 'y' above upper bound of 20

Error termination. Backtrace:
#0 0x102857577
...

```

i.e., you are told which of the two arrays and which dimension of it has the problem. If you do something (almost) similar in Julia:

```julia
n=20
x=Array{Float64}(undef,n)
y=Array{Float64}(undef,5*n)
dx=0.1
j=100
for i in range(1,n,step=1)
    x[i]=i*dx
    y[j]=3*x[j]
end

```

the error message is much more verbose but less clear (to me at least) and doesn’t say which of the two arrays in the flawed line is the culprit:

```julia
BoundsError: attempt to access 20-element Vector{Float64} at index [100]

Stacktrace:
 [1] getindex(A::Vector{Float64}, i1::Int64)
   @ Base ./array.jl:801
 [2] top-level scope
   @ ./In[6]:8
 [3] eval
   @ ./boot.jl:360 [inlined]
 [4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1094

```

This is in spite of having given `y` the length 100 in the Julia version to make the source of the error unambiguous; if I hadn’t done that, the error message would still be the same.  
Of course, here debugging is not such a bit challenge, but you can easily imagine more crowded expressions where it becomes a pain to sort out this type of problem.  
I don’t know if it is possible to enable Julia to provide this additional information without sacrificing performance. I should think that it must be feasible, because after all it must know which array has exceeded its bounds. Or is there already a means to do that?  
At any rate, I hope this illustrates my point.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [August 11, 2021, 11:21pm UTC](https://discourse.julialang.org/t/which-reference-is-undefined-here/65707/10 "2021-08-11T23:21:03Z")

</div>

I’m not sure there is a way to give you the “name” of the array: `getindex` is a regular function which sees the value of its arguments, not their “names”

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [August 11, 2021, 11:38pm UTC](https://discourse.julialang.org/t/which-reference-is-undefined-here/65707/11 "2021-08-11T23:38:30Z")

</div>

It’s a value, there is a name referring to the value in the parent frame at the call site in the appropriate argument position, I don’t see why it’s not possible to connect the two…at least in theory.

Is that this issue @tim.holy ? [https://github.com/JuliaLang/julia/issues/31162](https://github.com/JuliaLang/julia/issues/31162)

In general having error messages expressed in terms of user code is best.
