# RMagick 1.9.3: examples/histogram.rb - erro

**URL:** <https://www.ruby-forum.com/t/rmagick-1-9-3-examples-histogram-rb-erro/47831>\
**Category:** Ruby\
**Created:** [November 14, 2005, 7:09am UTC](https://www.ruby-forum.com/t/rmagick-1-9-3-examples-histogram-rb-erro/47831 "2005-11-14T07:09:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ces.fci.junk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@ces.fci.junk](https://www.ruby-forum.com/u/ces.fci.junk)\
**Post date:** [November 14, 2005, 7:09am UTC](https://www.ruby-forum.com/t/rmagick-1-9-3-examples-histogram-rb-erro/47831/1 "2005-11-14T07:09:26Z")

</div>

Trying to run the histogram.rb ~/$myimage produces the following error:

Creating b194000514\_7064\_Histogram.miffhistogram.rb:180:in `histogram': undefined method`+’ for nil:NilClass (NoMethodError)  
from histogram.rb:179:in `each' from histogram.rb:179:in`histogram’  
from histogram.rb:177:in `times' from histogram.rb:177:in`histogram’  
from histogram.rb:280

For the moment commenting these lines will let it run and output a  
viewable miff (beginning at line 180):  
#red[pixel.red] += 1  
#green[pixel.green] += 1  
#blue[pixel.blue] += 1

```
                # Only count opacity channel if some pixels are not

```

opaque.  
if !opaque?  
alpha[pixel.opacity] += 1  
end  
v = pixel\_intensity(pixel)  
#int[v] += 1

Potentially relevent (or not relevent) information:  
Ruby 1.8.2  
ImageMagick 6.1.9  
Linux 2.6.10

The other examples appear to run without issue.  
I am also relatively new to Ruby if that matters.

I tried adding these in the appropiate locations (figured it may have  
had problems adding to nil):  
red[pixel.red] = 0 if red[pixel.red].nil?  
blue[pixel.blue] = 0 if blue[pixel.blue].nil?  
green[pixel.green] = 0 if green[pixel.green].nil?  
#…  
alpha[pixel.opacity] = 0 if alpha[pixel.opacity].nil?  
#…  
int[v] = 0 if int[v].nil?

The result is another error, but I am not sure how to get around it:  
Creating b194000514\_7064\_Histogram.miff.histogram.rb:5:in `max': undefined method`\<=\>’ for nil:NilClass (NoMethodError)  
from histogram.rb:208:in `each' from histogram.rb:208:in`max’  
from histogram.rb:208:in `histogram’  
from histogram.rb:292

any thoughts on the matter?

Clayton

---

<div class="post-metadata">

**Author:** ![ces.fci.junk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@ces.fci.junk](https://www.ruby-forum.com/u/ces.fci.junk)\
**Post date:** [November 14, 2005, 2:13pm UTC](https://www.ruby-forum.com/t/rmagick-1-9-3-examples-histogram-rb-erro/47831/2 "2005-11-14T14:13:50Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Trying to run the histogram.rb ~/$myimage produces the following error:
> 
> Creating b194000514\_7064\_Histogram.miffhistogram.rb:180:in `histogram': undefined method`+’ for nil:NilClass (NoMethodError)  
> from histogram.rb:179:in `each' from histogram.rb:179:in`histogram’  
> from histogram.rb:177:in `times' from histogram.rb:177:in`histogram’  
> from histogram.rb:280

Hmmm…I get it, too. I should I say I “got” it, because after I  
started investigating the problem I started getting other very odd  
errors and then it started working. Now I can’t make it happen anymore.  
These are classic symptoms of a wild pointer either in RMagick or  
ImageMagick. I’ll poke around a little bit more. Whatever the problem  
is, it can’t be fixed by changing the script.

Thanks for pointing this out.

---

<div class="post-metadata">

**Author:** ![ces.fci.junk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@ces.fci.junk](https://www.ruby-forum.com/u/ces.fci.junk)\
**Post date:** [November 14, 2005, 5:20pm UTC](https://www.ruby-forum.com/t/rmagick-1-9-3-examples-histogram-rb-erro/47831/3 "2005-11-14T17:20:18Z")

</div>

I believe I figured it out although I’m not entirely sure of the  
solution (unless you can tell me how to loop through all the values and  
unset the nil values – note: i’m still new to ruby).  
running this lets me know there is a lot of nil values which would  
explain the “nil:NilClass (NoMethodError)” – nil doesn’t have the max  
method:  
for i in red  
p “nil!” if i.nil?  
end

Now, what is strange about this is that the arrays are _already_  
defined to all have zero:  
red = Array.new(HISTOGRAM\_COLS, 0)  
green = Array.new(HISTOGRAM\_COLS, 0)  
blue = Array.new(HISTOGRAM\_COLS, 0)  
alpha = Array.new(HISTOGRAM\_COLS, 0)  
int = Array.new(HISTOGRAM\_COLS, 0)

I tried changing all of the 0’s to 1’s to see if that would make a  
difference (it did not). I also tried using the fill method which had  
no effect.

but, if you say it is a wild pointer I’ll concede.

I had to download xwindow.c and xwindow.h from the sourceforge page  
since they weren’t included with my source[for ImageMagick] – I had  
received a compile error which said they were missing (maybe that is  
related[?]).

Clayton

---

<div class="post-metadata">

**Author:** ![ces.fci.junk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@ces.fci.junk](https://www.ruby-forum.com/u/ces.fci.junk)\
**Post date:** [November 15, 2005, 12:59am UTC](https://www.ruby-forum.com/t/rmagick-1-9-3-examples-histogram-rb-erro/47831/4 "2005-11-15T00:59:48Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I believe I figured it out although I’m not entirely sure of the  
> solution (unless you can tell me how to loop through all the values and  
> unset the nil values – note: i’m still new to ruby).  
> running this lets me know there is a lot of nil values which would  
> explain the “nil:NilClass (NoMethodError)” – nil doesn’t have the max  
> method:  
> for i in red  
> p “nil!” if i.nil?  
> end

Would you mind issuing this little command and sending me the output?

ruby -r rmagick -e’p Magick::Long\_version’

Thanks!

---

<div class="post-metadata">

**Author:** ![ces.fci.junk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@ces.fci.junk](https://www.ruby-forum.com/u/ces.fci.junk)\
**Post date:** [November 15, 2005, 3:54am UTC](https://www.ruby-forum.com/t/rmagick-1-9-3-examples-histogram-rb-erro/47831/5 "2005-11-15T03:54:12Z")

</div>

Here’s the output:  
% ruby -r RMagick -e’puts Magick::Long\_version’  
This is RMagick 1.9.3 ($Date: 2005/10/13 23:01:29 $) Copyright (C) 2005  
by Timothy P. Hunter  
Built with ImageMagick 6.1.9 01/20/05 Q16 [http://www.imagemagick.org](http://www.imagemagick.org)  
Built for ruby 1.8.2 (2004-12-25) [i686-linux]  
Web page: [http://rmagick.rubyforge.org](http://rmagick.rubyforge.org)  
Email: removed\_email\_address@domain.invalid

Clayton

---

<div class="post-metadata">

**Author:** ![ces.fci.junk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@ces.fci.junk](https://www.ruby-forum.com/u/ces.fci.junk)\
**Post date:** [November 14, 2005, 11:44pm UTC](https://www.ruby-forum.com/t/rmagick-1-9-3-examples-histogram-rb-erro/47831/6 "2005-11-14T23:44:37Z")

</div>

> Now, what is strange about this is that the arrays are _already_  
> defined to all have zero:

I’m reasonably sure there’s nothing wrong with the script itself, at  
least sure enought that it’s not at the top of my list for possible  
causes. I’ll investigate.
