# Keep getting undefined method \`length' for nil:NilClass (NoMethodError) error

**URL:** <https://www.ruby-forum.com/t/keep-getting-undefined-method-length-for-nil-nilclass-nomethoderror-error/259482>\
**Category:** Uncategorized\
**Created:** [May 5, 2020, 12:57pm UTC](https://www.ruby-forum.com/t/keep-getting-undefined-method-length-for-nil-nilclass-nomethoderror-error/259482 "2020-05-05T12:57:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ibz95](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/74df32/32.png) [@ibz95](https://www.ruby-forum.com/u/ibz95)\
**Post date:** [May 5, 2020, 12:57pm UTC](https://www.ruby-forum.com/t/keep-getting-undefined-method-length-for-nil-nilclass-nomethoderror-error/259482/1 "2020-05-05T12:57:16Z")

</div>

I have written the following code in Ruby that takes input from a user regarding plane and flight details and displays the user inputs as an array in the terminal. For example, if the user enters 3 then the program reads in input 3 times. However, I am getting the following error but I am not sure why: Traceback (most recent call last):  
2: from plane.rb:68:in `<main>' 1: from plane.rb:65:in `main’  
plane.rb:56:in `print_planes': undefined method `length’ for nil:NilClass (NoMethodError)

> Blockquote  
> require ‘./input\_functions’  
> class Plane  
> attr\_accessor :id, :number, :origin, :destination

```
def initialize (id, number, origin, destination)
	@id=id
    @number = number
    @origin = origin
    @destination = destination
end 

```

end

def read\_plane()

```
plane_id = read_integer("Enter plane id:")
plane_number = read_string("Enter flight name:")
plane_origin = read_string("Enter origin airport:")
plane_destination = read_string("Enter destination airport:")
plane = Plane.new(plane_id, plane_number, plane_origin, plane_destination)
plane.id = plane_id
plane.number = plane_number
plane.origin = plane_origin
plane.destination = plane_destination
return plane

```

end

def read\_planes()  
planes=Array.new()  
planes\_count = read\_integer(“How many planes are you entering?”)  
count=0  
while (count\<planes\_count)  
planes\<\<read\_plane()  
count+=1  
end

end

def print\_plane(plane)  
puts ("Plane id "+plane.id)  
puts ("Flight "+plane.number)  
puts ("Origin "+plane.origin)  
puts ("Destination "+plane.destination)

end

def print\_planes(planes)  
index = 0  
while (index\<planes.length)  
puts planes[index]  
index+=1  
end

end

def main()  
planes = read\_planes()  
print\_planes(planes)  
end

main()

---

<div class="post-metadata">

**Author:** ![pcl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pcl](https://www.ruby-forum.com/u/pcl)\
**Post date:** [May 5, 2020, 2:25pm UTC](https://www.ruby-forum.com/t/keep-getting-undefined-method-length-for-nil-nilclass-nomethoderror-error/259482/2 "2020-05-05T14:25:23Z")

</div>

Your method `read_planes` does not return the array of `planes`. Hence your `main` method gets `nil` in `planes`, and `print_planes` is receiving `nil`.

---

<div class="post-metadata">

**Author:** ![ibz95](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/74df32/32.png) [@ibz95](https://www.ruby-forum.com/u/ibz95)\
**Post date:** [May 5, 2020, 2:45pm UTC](https://www.ruby-forum.com/t/keep-getting-undefined-method-length-for-nil-nilclass-nomethoderror-error/259482/4 "2020-05-05T14:45:57Z")

</div>

Thank you for your reply, pcl. I have put in the return statement as you suggested. But instead of the program printing out the user input in the terminal I am getting this “#Plane:0x0000557c9678a880”. Why is this happening?

---

<div class="post-metadata">

**Author:** ![pcl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pcl](https://www.ruby-forum.com/u/pcl)\
**Post date:** [May 5, 2020, 2:55pm UTC](https://www.ruby-forum.com/t/keep-getting-undefined-method-length-for-nil-nilclass-nomethoderror-error/259482/5 "2020-05-05T14:55:19Z")

</div>

In print\_planes, you write `puts planes[index]` - there is no to\_s method in your class `Plane`, so it uses the default string representation, as you see!

But I think you meant to call `print_plane` instead of puts …

---

<div class="post-metadata">

**Author:** ![ibz95](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/74df32/32.png) [@ibz95](https://www.ruby-forum.com/u/ibz95)\
**Post date:** [May 5, 2020, 3:31pm UTC](https://www.ruby-forum.com/t/keep-getting-undefined-method-length-for-nil-nilclass-nomethoderror-error/259482/6 "2020-05-05T15:31:47Z")

</div>

Hi, pcl  
Thank you for replying again. I have tried doing as you suggested by doing the following:  
def print\_planes(planes)  
index = 0  
while (index\<planes.length)  
print\_plane  
index+=1  
end

But I now get an argument error. I am still very new to ruby so I can’t figure out how to fix this.

---

<div class="post-metadata">

**Author:** ![pcl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pcl](https://www.ruby-forum.com/u/pcl)\
**Post date:** [May 5, 2020, 4:23pm UTC](https://www.ruby-forum.com/t/keep-getting-undefined-method-length-for-nil-nilclass-nomethoderror-error/259482/7 "2020-05-05T16:23:54Z")

</div>

> But I now get an argument error. I am still very new to ruby so I can’t figure out how to fix this.

Take it step by step: What does your `print_plane` method do? It takes a plane as an argument, and prints its details.

What does your `print_planes` method do? It takes a list of planes as an argument, looks at each one, and prints its details, by calling `print_plane`. index is used to refer to each plane in turn, and `planes[index]` is the current plane.

What are you missing? You have not passed the current plane to `print_plane` - you were almost there with `puts planes[index]`, instead write:

`print_plane planes[index]`

You will run into a new problem here: because the @id is an integer, it can’t be printed directly but must be converted into a string. You can add `to_s` to each line in `print_plane`, like below:

```auto
puts ("Plane id "+plane.id.to_s)

```
