# MethodError: no method matching getindex(::Nothing, ::String)

**URL:** <https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-nothing-string/67080>\
**Category:** General Usage\
**Tags:** strings, error, dictionary, error-message\
**Created:** [August 27, 2021, 4:22am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-nothing-string/67080 "2021-08-27T04:22:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_Mena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sebastian_mena/32/22819_2.png) [@Sebastian\_Mena](https://discourse.julialang.org/u/Sebastian_Mena)\
**Post date:** [August 27, 2021, 4:22am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-nothing-string/67080/1 "2021-08-27T04:22:57Z")

</div>

Hi, I’m trying to do an iteration, where I use a dictionary that gives me an array with values (“players”) and an array with weights (“weights”) with the same size, to make a sample. When I do it outside of iteration it has no problem, but on the first iteration it throws me this error: ` MethodError: no method matchinggetindex (:: Nothing, :: String)`

Here is the code that works outside the iteration, but inside throws the error above.

```julia
new_player = sample(my_dict["2499719"][1631]["Z4.2"][14763]["players"],
                        Weights(my_dict["2499719"][1631]["Z4.2"][14763]["weights"]))

```

The complete info of the error is:

```julia
MethodError(getindex, (nothing, "2499719"), 0x0000000000006d1a)
MethodError: no method matching getindex(::Nothing, ::String)

Stacktrace:
 [1] my_function(::String, ::Int64, ::Int64) at .\In[44]:91
 [2] top-level scope at In[45]:1
 [3] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

```

I hope you can help me!

---

<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 27, 2021, 5:50am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-nothing-string/67080/2 "2021-08-27T05:50:14Z")

</div>

Your `my_dict` variable is `nothing`.

For more help, please post a self contained, minimum (non-)working example.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![Sebastian\_Mena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sebastian_mena/32/22819_2.png) [@Sebastian\_Mena](https://discourse.julialang.org/u/Sebastian_Mena)\
**Post date:** [August 27, 2021, 11:53pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-nothing-string/67080/3 "2021-08-27T23:53:26Z")

</div>

I did not understand, how is it `nothing`? if it worked outside the for loop.  
PS: your link doesn’t work.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 28, 2021, 12:33am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-getindex-nothing-string/67080/4 "2021-08-28T00:33:10Z")

</div>

> [@Sebastian\_Mena](#):
>
> your link doesn’t work.

how?

> [@Sebastian\_Mena](#):
>
> if it worked outside the for loop.

can you show a runnable code?
