NoMethodError - undefined method `[]' for nil:NilClass


#1

Hi,

How do I prevent the below from happening? I thought given the below logic
it will always work.

I am trying to set node[‘kafka’][‘brokerid’] only once. How do I get
NoMethodError?

44:
45>> if node[‘kafka’][‘brokerid’].nil?
46: node.set[‘kafka’][‘brokerid’] = Time.now.getutc.to_i
47: end
48: brokerid = node[‘kafka’][‘brokerid’]
49:
50: template
"/var/lib/kafka-0.7.2-incubating-src/config/server.properties" do
51: path "/var/lib/kafka-0.7.2-incubating-src/config/server.properties"
52: source "server.properties.erb"
53: owner "root"
54: group “root”

[2013-07-16T22:48:42+00:00] DEBUG: Re-raising exception: NoMethodError -
undefined method `[]’ for nil:NilClass


#2

node[‘kafka’] is nil, you need to initialize that hash before attempting access a key within it. Add default[:kafka] = Hash.new or default[:kafka][:broker_id] = false (and remove the .nil? from the recipe) to cookbooks/kafka/attributes/default.rb

On Tuesday, July 16, 2013 at 4:00 PM, David Montgomery wrote:

Hi,

How do I prevent the below from happening? I thought given the below logic it will always work.

I am trying to set node[‘kafka’][‘brokerid’] only once. How do I get NoMethodError?

44:
45>> if node[‘kafka’][‘brokerid’].nil?
46: node.set[‘kafka’][‘brokerid’] = Time.now.getutc.to_i
47: end
48: brokerid = node[‘kafka’][‘brokerid’]
49:
50: template “/var/lib/kafka-0.7.2-incubating-src/config/server.properties” do
51: path "/var/lib/kafka-0.7.2-incubating-src/config/server.properties"
52: source "server.properties.erb"
53: owner "root"
54: group “root”

[2013-07-16T22:48:42+00:00] DEBUG: Re-raising exception: NoMethodError - undefined method `[]’ for nil:NilClass


#3

You are referencing a missing node[‘kafka’] hash, probably because you
haven’t included kafka as a dependency of this other cookbook.


Nathaniel Eliot
T9 Productions
On Jul 16, 2013 6:00 PM, “David Montgomery” davidmontgomery@gmail.com
wrote:

Hi,

How do I prevent the below from happening? I thought given the below
logic it will always work.

I am trying to set node[‘kafka’][‘brokerid’] only once. How do I get
NoMethodError?

44:
45>> if node[‘kafka’][‘brokerid’].nil?
46: node.set[‘kafka’][‘brokerid’] = Time.now.getutc.to_i
47: end
48: brokerid = node[‘kafka’][‘brokerid’]
49:
50: template
"/var/lib/kafka-0.7.2-incubating-src/config/server.properties" do
51: path "/var/lib/kafka-0.7.2-incubating-src/config/server.properties"
52: source "server.properties.erb"
53: owner "root"
54: group “root”

[2013-07-16T22:48:42+00:00] DEBUG: Re-raising exception: NoMethodError -
undefined method `[]’ for nil:NilClass


#4

few days back we had a similar thread on checking nested attributes , chef
has a helper to do it for knife plugins method


,
you can use the same logic before accessing or setting a nested atttribute

On Tue, Jul 16, 2013 at 4:03 PM, Nathaniel Eliot <temujin9@t9productions.com

wrote:

You are referencing a missing node[‘kafka’] hash, probably because you
haven’t included kafka as a dependency of this other cookbook.


Nathaniel Eliot
T9 Productions
On Jul 16, 2013 6:00 PM, “David Montgomery” davidmontgomery@gmail.com
wrote:

Hi,

How do I prevent the below from happening? I thought given the below
logic it will always work.

I am trying to set node[‘kafka’][‘brokerid’] only once. How do I get
NoMethodError?

44:
45>> if node[‘kafka’][‘brokerid’].nil?
46: node.set[‘kafka’][‘brokerid’] = Time.now.getutc.to_i
47: end
48: brokerid = node[‘kafka’][‘brokerid’]
49:
50: template
"/var/lib/kafka-0.7.2-incubating-src/config/server.properties" do
51: path
"/var/lib/kafka-0.7.2-incubating-src/config/server.properties"
52: source "server.properties.erb"
53: owner "root"
54: group “root”

[2013-07-16T22:48:42+00:00] DEBUG: Re-raising exception: NoMethodError -
undefined method `[]’ for nil:NilClass


#5

oic…that fixed it. Thanks

On Wed, Jul 17, 2013 at 7:03 AM, Nathaniel Eliot <temujin9@t9productions.com

wrote:

You are referencing a missing node[‘kafka’] hash, probably because you
haven’t included kafka as a dependency of this other cookbook.


Nathaniel Eliot
T9 Productions
On Jul 16, 2013 6:00 PM, “David Montgomery” davidmontgomery@gmail.com
wrote:

Hi,

How do I prevent the below from happening? I thought given the below
logic it will always work.

I am trying to set node[‘kafka’][‘brokerid’] only once. How do I get
NoMethodError?

44:
45>> if node[‘kafka’][‘brokerid’].nil?
46: node.set[‘kafka’][‘brokerid’] = Time.now.getutc.to_i
47: end
48: brokerid = node[‘kafka’][‘brokerid’]
49:
50: template
"/var/lib/kafka-0.7.2-incubating-src/config/server.properties" do
51: path
"/var/lib/kafka-0.7.2-incubating-src/config/server.properties"
52: source "server.properties.erb"
53: owner "root"
54: group “root”

[2013-07-16T22:48:42+00:00] DEBUG: Re-raising exception: NoMethodError -
undefined method `[]’ for nil:NilClass