# Printing nil objects

**URL:** <https://www.ruby-forum.com/t/printing-nil-objects/105118>\
**Category:** Ruby\
**Created:** [May 25, 2007, 10:25pm UTC](https://www.ruby-forum.com/t/printing-nil-objects/105118 "2007-05-25T22:25:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![finer\_recliner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f07891/32.png) [@finer\_recliner](https://www.ruby-forum.com/u/finer_recliner)\
**Post date:** [May 25, 2007, 10:25pm UTC](https://www.ruby-forum.com/t/printing-nil-objects/105118/1 "2007-05-25T22:25:46Z")

</div>

hey, im pretty new to ruby and im having trouble. this is not my  
original code (much longer), but i quickly rewrote the part of the  
data structure that was creating the problem so i could post it here:

i have a node class that is used to make a linked list. the node  
object has a value, a pointer to the next node in the list, and  
another node that is sometimes used to point to another node in the  
list somewhere.  
when pointer2 is not used it is set to nil. However when i try to  
print out my linked list (which will print each node’s values and  
where its pointers are pointing) it crashes when it reaches a pointer  
set to nil.

i tried making an if-then-else check, but it doesnt seem to work. can  
anyone help me? thanks in advance.

```auto
class Node

    def initialize(v,p1,p2)
        @value = v
        @pointer1 = p1 #next node
        @pointer2 = p2 #points to another node
    end

    def getP1
        @pointer1
    end

    def setP1(x)
        @pointer1 = x
    end

    def getP2
        @pointer2
    end

    def setP2(x)
        @pointer2 = x
    end

    def getV
        @value
    end

    def setV(x)
        @value = x
    end

end

### code starts here ###
c1 = Node.new(1,nil,nil)
c2 = Node.new(2,nil,nil)
c3 = Node.new(3,nil,nil)

c1.setP1(c2)
c1.setP2(c2)
c2.setP1(c3)

current = c1
while current.getP1 != nil
    print "value = #{current.getV}, pointer1 = #{current.getP1.getV},
"
    if current.getP2.getV != nil then print "pointer2 =
#{current.getP2.getV}\n" else print "null\n" end
    current = current.getP1
end
```

output:  
$ ruby node.rb  
value = 1, pointer1 = 2, pointer2 = 2  
node.rb:49: undefined method `getV’ for nil:NilClass (NoMethodError)  
value = 2, pointer1 = 3,  
$

---

<div class="post-metadata">

**Author:** ![finer\_recliner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f07891/32.png) [@finer\_recliner](https://www.ruby-forum.com/u/finer_recliner)\
**Post date:** [May 25, 2007, 10:31pm UTC](https://www.ruby-forum.com/t/printing-nil-objects/105118/2 "2007-05-25T22:31:46Z")

</div>

> current = c1  
> while current.getP1 != nil  
> print "value = #{current.getV}, pointer1 = #{current.getP1.getV},  
> "  
> if current.getP2.getV != nil then print “pointer2 =  
> #{current.getP2.getV}\n” else print “null\n” end  
> current = current.getP1  
> end  
> [/code]

Your problem lies exactly there. You are trying to make a method call  
(getV) on a nil object, and obviously it doesn’t have the (getV)  
method…

Try the following

if current.getP2.nil? then…

That should work.

Cheers,

* * *

Enrique Comba R.  
removed\_email\_address@domain.invalid

I always thought Smalltalk would beat Java, I just didn’t know it  
would be called ‘Ruby’ when it did.  
– Kent Beck

---

<div class="post-metadata">

**Author:** ![finer\_recliner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f07891/32.png) [@finer\_recliner](https://www.ruby-forum.com/u/finer_recliner)\
**Post date:** [May 25, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/printing-nil-objects/105118/3 "2007-05-25T22:40:47Z")

</div>

On May 25, 4:31 pm, Enrique Comba R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > while current.getP1 != nil  
> > method…  
> > Enrique Comba R.  
> > removed\_email\_address@domain.invalid
> 
> I always thought Smalltalk would beat Java, I just didn’t know it  
> would be called ‘Ruby’ when it did.  
> – Kent Beck

ooooh. now i feel foolish lol. thanks!

---

<div class="post-metadata">

**Author:** ![finer\_recliner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f07891/32.png) [@finer\_recliner](https://www.ruby-forum.com/u/finer_recliner)\
**Post date:** [May 25, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/printing-nil-objects/105118/4 "2007-05-25T22:51:48Z")

</div>

On 25 May 2007, at 22:40, finer recliner wrote:

> > > current = c1  
> > > Your problem lies exactly there. You are trying to make a method call
> > 
> > * * *
> > 
> > Enrique Comba R.  
> > removed\_email\_address@domain.invalid
> > 
> > I always thought Smalltalk would beat Java, I just didn’t know it  
> > would be called ‘Ruby’ when it did.  
> > – Kent Beck
> 
> ooooh. now i feel foolish lol. thanks!

Why should you? No one is perfect and sometimes when in the middle of  
the brain crunching activity we oversee the simplest things… 🙂

Cheers,

* * *

Enrique Comba R.  
removed\_email\_address@domain.invalid

I always thought Smalltalk would beat Java, I just didn’t know it  
would be called ‘Ruby’ when it did.  
– Kent Beck
