# Keep getting an error saying that this array value is nil, h

**URL:** <https://www.ruby-forum.com/t/keep-getting-an-error-saying-that-this-array-value-is-nil-h/108754>\
**Category:** Ruby\
**Created:** [July 6, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/keep-getting-an-error-saying-that-this-array-value-is-nil-h/108754 "2007-07-06T18:26:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shadowcipher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@shadowcipher](https://www.ruby-forum.com/u/shadowcipher)\
**Post date:** [July 6, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/keep-getting-an-error-saying-that-this-array-value-is-nil-h/108754/1 "2007-07-06T18:26:11Z")

</div>

elemkeysraw = str.split(/=(?!\>)/)  
i = elemkeysraw.length  
scr.printw(“This is the value of hashargs is #{hashargs} and it’s class  
is  
#{hashargs.class} and elemkeysraw[i] is #{elemkeysraw[i]}  
elemkeysraw[i].split(/=\>/)[0] is #{elemkeysraw[i].split(/=\>/)[0]}”)

This is the chunk of code, and the elemkeysraw[i].split(/=\>/)[0] causes  
an  
error;

private method `split’ called for nil:NilClass (NoMethodError)

However elemkeysraw[i] provides a value. What’s the deal with this?  
I’m  
guessing it is because the fact that “i” is not evaluated until runtime,  
but  
why does the element reference work, but not when attempting to use a  
string  
method on the value that is contained there?

---

<div class="post-metadata">

**Author:** ![shadowcipher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@shadowcipher](https://www.ruby-forum.com/u/shadowcipher)\
**Post date:** [July 6, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/keep-getting-an-error-saying-that-this-array-value-is-nil-h/108754/2 "2007-07-06T18:46:55Z")

</div>

Nathan T. wrote:

> elemkeysraw = str.split(/=(?!\>)/)  
> i = elemkeysraw.length  
> scr.printw(“This is the value of hashargs is #{hashargs} and it’s class  
> is  
> #{hashargs.class} and elemkeysraw[i] is #{elemkeysraw[i]}  
> elemkeysraw[i].split(/=\>/)[0] is #{elemkeysraw[i].split(/=\>/)[0]}”)
> 
> This is the chunk of code, and the elemkeysraw[i].split(/=\>/)[0] causes  
> an  
> error;
> 
> private method `split’ called for nil:NilClass (NoMethodError)
> 
> However elemkeysraw[i] provides a value. What’s the deal with this?  
> I’m  
> guessing it is because the fact that “i” is not evaluated until runtime,  
> but  
> why does the element reference work, but not when attempting to use a  
> string  
> method on the value that is contained there?

Nil is a value. [][5] # =\> nil; “#{nil}” # =\> “”  
Instead of wild guessing, I suggest you do #{elemkeysraw[i].inspect}  
instead of just #{elemkeysraw[i]}. That gives you a better picture what  
you really have, since #{} calls to\_s on the value it encloses.

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![shadowcipher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@shadowcipher](https://www.ruby-forum.com/u/shadowcipher)\
**Post date:** [July 6, 2007, 7:29pm UTC](https://www.ruby-forum.com/t/keep-getting-an-error-saying-that-this-array-value-is-nil-h/108754/3 "2007-07-06T19:29:21Z")

</div>

If i equals the length of the array, elemkeysraw[i] would be out of  
range  
and return a nil value and trigger the error, no?  
elemkeysraw[i-1] gets the last value in array.  
Not sure why it would be returning a value earlier though.

---

<div class="post-metadata">

**Author:** ![shadowcipher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@shadowcipher](https://www.ruby-forum.com/u/shadowcipher)\
**Post date:** [July 6, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/keep-getting-an-error-saying-that-this-array-value-is-nil-h/108754/4 "2007-07-06T20:14:25Z")

</div>

Yes, brainfart for me. Elements start at 0. Doh!
