You have a nil object when you didn't expect it!

Dear friends,

I get the following error when I goto the privilege page.

NoMethodError in Employee#edit_privilege

Showing app/views/employee/edit_privilege.html.erb where line #82
raised:

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
Extracted source (around line #82):

79: <table width="100%" align="center" cellspacing="1"
cellpadding="1" class="table_design">
80: <tr>
81: <% @privilege_tags.each_with_index do

privilege_tag,ind| %>

82: <% all_privileges =
privilege_tag.privileges.all(:conditions=>"name<>'FinanceControl'",:order=>"priority
ASC").select{|p| p.applicable?} %>
83: <% if all_privileges.count > 0 %>
84: <% if ind==0 or ind==1 or ind==4 %>
85: <td>

The development log is below:

Processing EmployeeController#edit_privilege (for 127.0.0.1 at
2016-03-26 13:49:00) [GET]
  Parameters: {"action"=>"edit_privilege", "controller"=>"employee",
"id"=>"emp-0002"}
  [4;35;1mUser Load (1.0ms)[0m [0mSELECT * FROM `users` WHERE
(`users`.`id` = 1) AND (`users`.`is_deleted` = 0) [0m
  [4;36;1mConfiguration Load (1.0ms)[0m [0;1mSELECT * FROM
`configurations` WHERE (`configurations`.`config_key` = 'Locale') LIMIT
1[0m
  [4;35;1mConfiguration Load (1.0ms)[0m [0mSELECT * FROM
`configurations` WHERE (`configurations`.`config_key` =
'InstitutionType') LIMIT 1[0m
Expired fragment: views/News_latest_fragment (0.0ms)
  [4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE
(`users`.`id` = 1) AND (`users`.`is_deleted` = 0) [0m
Username : admin Role : Admin
  [4;35;1mConfiguration Load (0.0ms)[0m [0mSELECT * FROM
`configurations` WHERE (`configurations`.`config_key` =
'StudentAttendanceType') LIMIT 1[0m
  [4;36;1mConfiguration Load (0.0ms)[0m [0;1mSELECT * FROM
`configurations` WHERE (`configurations`.`config_key` =
'AvailableModules') [0m
  [4;35;1mUser Load (0.0ms)[0m [0mSELECT * FROM `users` WHERE
(`users`.`id` = 1) [0m
  [4;36;1mConfiguration Load (1.0ms)[0m [0;1mSELECT * FROM
`configurations` WHERE (`configurations`.`config_key` =
'FirstTimeLoginEnable') LIMIT 1[0m
  [4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE
(`users`.`id` = 1) AND (`users`.`is_deleted` = 0) [0m
  [4;36;1mConfiguration Load (1.0ms)[0m [0;1mSELECT * FROM
`configurations` WHERE (`configurations`.`config_value` = 'HR') LIMIT
1[0m
  [4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `users` WHERE
(`users`.`id` = 1) AND (`users`.`is_deleted` = 0) [0m
  [4;36;1mPrivilege Load (0.0ms)[0m [0;1mSELECT * FROM `privileges`
INNER JOIN `privileges_users` ON `privileges`.id =
`privileges_users`.privilege_id WHERE (`privileges_users`.user_id = 1 )
[0m
  [4;35;1mConfiguration Load (1.0ms)[0m [0mSELECT * FROM
`configurations` WHERE (`configurations`.`config_key` =
'PrecisionCount') LIMIT 1[0m
  [4;36;1mUser Load (1.0ms)[0m [0;1mSELECT * FROM `users` WHERE
(username LIKE BINARY('emp-0002')) AND (`users`.`is_deleted` = 0) LIMIT
1[0m
  [4;35;1mEmployee Load (1.0ms)[0m [0mSELECT * FROM `employees` WHERE
(`employees`.user_id = 4) LIMIT 1[0m
  [4;36;1mConfiguration Load (1.0ms)[0m [0;1mSELECT * FROM
`configurations` WHERE (`configurations`.`config_value` = 'Finance')
LIMIT 1[0m
  [4;35;1mSmsSetting Columns (2.0ms)[0m [0mSHOW FIELDS FROM
`sms_settings`[0m
  [4;36;1mSmsSetting Load (0.0ms)[0m [0;1mSELECT * FROM `sms_settings`
WHERE (`sms_settings`.`settings_key` = 'ApplicationEnabled') LIMIT 1[0m
  [4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `configurations` WHERE
(`configurations`.`config_value` = 'HR') LIMIT 1[0m
  [4;36;1mPrivilegeTag Load (0.0ms)[0m [0;1mSELECT * FROM
`privilege_tags` ORDER BY priority ASC[0m
Rendering template within layouts/application
Rendering employee/edit_privilege
  [4;35;1mEmployee Columns (3.0ms)[0m [0mSHOW FIELDS FROM
`employees`[0m
  [4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE
(`users`.`id` = 1) AND (`users`.`is_deleted` = 0) [0m
  [4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `privileges` INNER JOIN
`privileges_users` ON `privileges`.id = `privileges_users`.privilege_id
WHERE (`privileges_users`.user_id = 1 ) [0m
  [4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE
(`users`.`id` = 1) AND (`users`.`is_deleted` = 0) [0m
  [4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `privileges` INNER JOIN
`privileges_users` ON `privileges`.id = `privileges_users`.privilege_id
WHERE (`privileges_users`.user_id = 1 ) [0m
  [4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM `users` WHERE
(`users`.`id` = 1) AND (`users`.`is_deleted` = 0) [0m
  [4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM `privileges` INNER JOIN
`privileges_users` ON `privileges`.id = `privileges_users`.privilege_id
WHERE (`privileges_users`.user_id = 1 ) [0m
  [4;36;1mPrivilegeTag Columns (1.0ms)[0m [0;1mSHOW FIELDS FROM
`privilege_tags`[0m
  [4;35;1mPrivilege Load (0.0ms)[0m [0mSELECT * FROM `privileges`
WHERE (name<>'FinanceControl') AND (`privileges`.privilege_tag_id = 2)
ORDER BY priority ASC[0m
  [4;36;1mPrivilege Columns (2.0ms)[0m [0;1mSHOW FIELDS FROM
`privileges`[0m

ActionView::TemplateError (You have a nil object when you didn't expect
it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]) on line #82 of
app/views/employee/edit_privilege.html.erb:
79: <table width="100%" align="center" cellspacing="1"
cellpadding="1" class="table_design">
80: <tr>
81: <% @privilege_tags.each_with_index do

privilege_tag,ind| %>

82: <% all_privileges =
privilege_tag.privileges.all(:conditions=>"name<>'FinanceControl'",:order=>"priority
ASC").select{|p| p.applicable?} %>
83: <% if all_privileges.count > 0 %>
84: <% if ind==0 or ind==1 or ind==4 %>
85: <td>

    app/models/privilege.rb:24:in `applicable?'
    app/views/employee/edit_privilege.html.erb:82
    app/views/employee/edit_privilege.html.erb:82:in `select'
    app/views/employee/edit_privilege.html.erb:82
    C:/Ruby187/lib/ruby/gems/1.8/gems/i18n-0.4.2/lib/i18n/backend/base.rb:42:in
`each_with_index'
    app/views/employee/edit_privilege.html.erb:81:in `each'
    app/views/employee/edit_privilege.html.erb:81:in `each_with_index'
    app/views/employee/edit_privilege.html.erb:81
    app/views/employee/edit_privilege.html.erb:71
    app/controllers/application_controller.rb:361:in `render'

Rendered rescues/_trace (198.0ms)
Rendered rescues/_request_and_response (1.0ms)
Rendering rescues/layout (internal_server_error)

and my users table has the following record.

username: emp-0002
first_name: Adam
password: xxxxxxxx
etc.

can any one help me pls what this error means or what is it asking from
me.

The error says you have called the method [] on an object that is nil.
You don't seem to have shown us line 24 of privilege.rb (and the
surrounding lines).

Colin

Colin Law wrote in post #1182464:

You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

    app/models/privilege.rb:24:in `applicable?'

The error says you have called the method [] on an object that is nil.
You don't seem to have shown us line 24 of privilege.rb (and the
surrounding lines).

Colin

class Privilege < ActiveRecord::Base
  has_and_belongs_to_many :users
  belongs_to :privilege_tag

  def applicable?
    role_plugin_hash =
Authorization::Engine.instance.roles_plugin_hash[self.name.underscore.to_sym]
    role_plugin_hash.nil? ? false : (role_plugin_hash.include? nil) ?
true : (role_plugin_hash & FedenaPlugin.accessible_plugins).present? ?
true : false
  end
end

Colin Law wrote in post #1182464:

You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

    app/models/privilege.rb:24:in `applicable?'

The error says you have called the method [] on an object that is nil.
You don't seem to have shown us line 24 of privilege.rb (and the
surrounding lines).

Colin

class Privilege < ActiveRecord::Base
  has_and_belongs_to_many :users
  belongs_to :privilege_tag

  def applicable?
    role_plugin_hash =
Authorization::Engine.instance.roles_plugin_hash[self.name.underscore.to_sym]

You have not told us which is line 24, but I assume it is the one
above, in which case roles_plugin_hash is nil.

    role_plugin_hash.nil? ? false : (role_plugin_hash.include? nil) ?
true : (role_plugin_hash & FedenaPlugin.accessible_plugins).present? ?
true : false

That is horrible. A classic case of saving a few lines of code and
making it difficult to understand what the code is doing. I just hope
your automated tests check all the possible conditions on this.

Colin

Colin

Sorry below is line 24

  role_plugin_hash =
Authorization::Engine.instance.roles_plugin_hash[self.name.underscore.to_sym]

So Authorization::Engine.instance.roles_plugin_hash is nil.

Colin

So Authorization::Engine.instance.roles_plugin_hash is nil.

Colin

No idea what to do

It depends on whether that is ever supposed to be nil. If it is then
you just need to test for that in your code before using it.

Colin

It depends on whether that is ever supposed to be nil. If it is then
you just need to test for that in your code before using it.

Colin

In my controller's edit_privilege method I changed the below line

from
@user = User.active.first(:conditions => ["username LIKE
BINARY(?)",params[:id]])
to
@user = User.active.find_by_username(params[:id])

and it solved the problem. but dont know why not the above line working?

def edit_privilege
    @user = User.active.find_by_username(params[:id])
  #User.active.first(:conditions => ["username LIKE
BINARY(?)",params[:id]])
    @employee = @user.employee_record
@privilege_tags=PrivilegeTag.find(:all,:order=>"priority ASC")
    @user_privileges=@user.privileges
if request.post?
      new_privileges = params[:user][:privilege_ids] if params[:user]
      new_privileges ||= []
      @user.privileges = Privilege.find_all_by_id(new_privileges)
      redirect_to :action => 'admission4',:id => @employee.id
    end
  end

I would not have expected either to work, as I would have expected
params[:id] to contain the id of the user not the name. What is in
params[:id]?

Colin

Colin,

User.active.first(:conditions => ["username LIKE ?, params[:id])

The above code is wrong. here your are trying to fetch the list of the active users and fetching the first record

then checking the username with id value. the entire code seems wrong

also in params[:id] you have to get ID value and not the name, In that case you are passing the value wrong in form

Thanks,

Asvini Ramesh

Dear friends,

I get the following error when I goto the privilege page.

NoMethodError in Employee#edit_privilege

Showing app/views/employee/edit_privilege.html.erb where line #82

raised:

You have a nil object when you didn’t expect it!

You might have expected an instance of ActiveRecord::Base.

The error occurred while evaluating nil.[]

Extracted source (around line #82):

79: <table width=“100%” align=“center” cellspacing=“1”

cellpadding=“1” class=“table_design”>

80:

81: <% @privilege_tags.each_with_index do

privilege_tag,ind| %>

82: <% all_privileges =

privilege_tag.privileges.all(:conditions=>“name<>‘FinanceControl’”,:order=>"priority

ASC").select{|p| p.applicable?} %>

83: <% if all_privileges.count > 0 %>

84: <% if ind==0 or ind==1 or ind==4 %>

85:

The development log is below:

Processing EmployeeController#edit_privilege (for 127.0.0.1 at

2016-03-26 13:49:00) [GET]

Parameters: {“action”=>“edit_privilege”, “controller”=>“employee”,

“id”=>“emp-0002”}

[4;35;1mUser Load (1.0ms)[0m [0mSELECT * FROM users WHERE

(users.id = 1) AND (users.is_deleted = 0) [0m

[4;36;1mConfiguration Load (1.0ms)[0m [0;1mSELECT * FROM

configurations WHERE (configurations.config_key = ‘Locale’) LIMIT

1[0m

[4;35;1mConfiguration Load (1.0ms)[0m [0mSELECT * FROM

configurations WHERE (configurations.config_key =

‘InstitutionType’) LIMIT 1[0m

Expired fragment: views/News_latest_fragment (0.0ms)

[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM users WHERE

(users.id = 1) AND (users.is_deleted = 0) [0m

Username : admin Role : Admin

[4;35;1mConfiguration Load (0.0ms)[0m [0mSELECT * FROM

configurations WHERE (configurations.config_key =

‘StudentAttendanceType’) LIMIT 1[0m

[4;36;1mConfiguration Load (0.0ms)[0m [0;1mSELECT * FROM

configurations WHERE (configurations.config_key =

‘AvailableModules’) [0m

[4;35;1mUser Load (0.0ms)[0m [0mSELECT * FROM users WHERE

(users.id = 1) [0m

[4;36;1mConfiguration Load (1.0ms)[0m [0;1mSELECT * FROM

configurations WHERE (configurations.config_key =

‘FirstTimeLoginEnable’) LIMIT 1[0m

[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM users WHERE

(users.id = 1) AND (users.is_deleted = 0) [0m

[4;36;1mConfiguration Load (1.0ms)[0m [0;1mSELECT * FROM

configurations WHERE (configurations.config_value = ‘HR’) LIMIT

1[0m

[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM users WHERE

(users.id = 1) AND (users.is_deleted = 0) [0m

[4;36;1mPrivilege Load (0.0ms)[0m [0;1mSELECT * FROM privileges

INNER JOIN privileges_users ON privileges.id =

privileges_users.privilege_id WHERE (privileges_users.user_id = 1 )

[0m

[4;35;1mConfiguration Load (1.0ms)[0m [0mSELECT * FROM

configurations WHERE (configurations.config_key =

‘PrecisionCount’) LIMIT 1[0m

[4;36;1mUser Load (1.0ms)[0m [0;1mSELECT * FROM users WHERE

(username LIKE BINARY(‘emp-0002’)) AND (users.is_deleted = 0) LIMIT

1[0m

[4;35;1mEmployee Load (1.0ms)[0m [0mSELECT * FROM employees WHERE

(employees.user_id = 4) LIMIT 1[0m

[4;36;1mConfiguration Load (1.0ms)[0m [0;1mSELECT * FROM

configurations WHERE (configurations.config_value = ‘Finance’)

LIMIT 1[0m

[4;35;1mSmsSetting Columns (2.0ms)[0m [0mSHOW FIELDS FROM

sms_settings[0m

[4;36;1mSmsSetting Load (0.0ms)[0m [0;1mSELECT * FROM sms_settings

WHERE (sms_settings.settings_key = ‘ApplicationEnabled’) LIMIT 1[0m

[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM configurations WHERE

(configurations.config_value = ‘HR’) LIMIT 1[0m

[4;36;1mPrivilegeTag Load (0.0ms)[0m [0;1mSELECT * FROM

privilege_tags ORDER BY priority ASC[0m

Rendering template within layouts/application

Rendering employee/edit_privilege

[4;35;1mEmployee Columns (3.0ms)[0m [0mSHOW FIELDS FROM

employees[0m

[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM users WHERE

(users.id = 1) AND (users.is_deleted = 0) [0m

[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM privileges INNER JOIN

privileges_users ON privileges.id = privileges_users.privilege_id

WHERE (privileges_users.user_id = 1 ) [0m

[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM users WHERE

(users.id = 1) AND (users.is_deleted = 0) [0m

[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM privileges INNER JOIN

privileges_users ON privileges.id = privileges_users.privilege_id

WHERE (privileges_users.user_id = 1 ) [0m

[4;36;1mCACHE (0.0ms)[0m [0;1mSELECT * FROM users WHERE

(users.id = 1) AND (users.is_deleted = 0) [0m

[4;35;1mCACHE (0.0ms)[0m [0mSELECT * FROM privileges INNER JOIN

privileges_users ON privileges.id = privileges_users.privilege_id

WHERE (privileges_users.user_id = 1 ) [0m

[4;36;1mPrivilegeTag Columns (1.0ms)[0m [0;1mSHOW FIELDS FROM

privilege_tags[0m

[4;35;1mPrivilege Load (0.0ms)[0m [0mSELECT * FROM privileges

WHERE (name<>‘FinanceControl’) AND (privileges.privilege_tag_id = 2)

ORDER BY priority ASC[0m

[4;36;1mPrivilege Columns (2.0ms)[0m [0;1mSHOW FIELDS FROM

privileges[0m

ActionView::TemplateError (You have a nil object when you didn’t expect

it!

You might have expected an instance of ActiveRecord::Base.

The error occurred while evaluating nil.[]) on line #82 of

app/views/employee/edit_privilege.html.erb:

79: <table width=“100%” align=“center” cellspacing=“1”

cellpadding=“1” class=“table_design”>

80:

81: <% @privilege_tags.each_with_index do

privilege_tag,ind| %>

82: <% all_privileges =

privilege_tag.privileges.all(:conditions=>“name<>‘FinanceControl’”,:order=>"priority

ASC").select{|p| p.applicable?} %>

83: <% if all_privileges.count > 0 %>

84: <% if ind==0 or ind==1 or ind==4 %>

85:

app/models/privilege.rb:24:in `applicable?'

app/views/employee/edit_privilege.html.erb:82

app/views/employee/edit_privilege.html.erb:82:in `select'

app/views/employee/edit_privilege.html.erb:82

C:/Ruby187/lib/ruby/gems/1.8/gems/i18n-0.4.2/lib/i18n/backend/base.rb:42:in

`each_with_index’

app/views/employee/edit_privilege.html.erb:81:in `each'

app/views/employee/edit_privilege.html.erb:81:in `each_with_index'

app/views/employee/edit_privilege.html.erb:81

app/views/employee/edit_privilege.html.erb:71

app/controllers/application_controller.rb:361:in `render'

Rendered rescues/_trace (198.0ms)

Rendered rescues/_request_and_response (1.0ms)

Rendering rescues/layout (internal_server_error)

and my users table has the following record.

username: emp-0002

first_name: Adam

password: xxxxxxxx

etc.

can any one help me pls what this error means or what is it asking from

me.


Posted via http://www.ruby-forum.com/.

Asvini Ramesh wrote in post #1182593:

On Saturday, March 26, 2016 at 3:00:46 PM UTC+5:30, Ruby-Forum.com User

Well in Production mode it works fine but in Development mode it show
the above error.

What works fine? You have not shown us what the current code is.
Also show the full error message and the line of code giving the error
(with a few lines either side).

This is a mailing list not a forum (though you may be accessing it via
a forum like interface), so you must not force us to look back through
previous messages trying to work out what you are talking about,
particularly when your previous message was over a week ago.

Colin

Colin

I get the following error when I goto the privilege page.

NoMethodError in Employee#edit_privilege

Showing app/views/employee/edit_privilege.html.erb where line #82
raised:

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
Extracted source (around line #82):

79: <table width="100%" align="center" cellspacing="1"
cellpadding="1" class="table_design">
80: <tr>
81: <% @privilege_tags.each_with_index do

privilege_tag,ind| %>

82: <% all_privileges =
privilege_tag.privileges.all(:conditions=>"name<>'FinanceControl'",:order=>"priority
ASC").select{|p| p.applicable?} %>
83: <% if all_privileges.count > 0 %>
84: <% if ind==0 or ind==1 or ind==4 %>
85: <td>

The development log is below:

Processing EmployeeController#edit_privilege (for 127.0.0.1 at
2016-03-26 13:49:00) [GET]
  Parameters: {"action"=>"edit_privilege", "controller"=>"employee",
"id"=>"emp-0002"}
  [4;35;1mUser Load (1.0ms)[0m [0mSELECT * FROM `users` WHERE
(`users`.`id` = 1) AND (`users`.`is_deleted` = 0) [0m
  [4;36;1mConfiguration Load (1.0ms)[0m [0;1mSELECT * FROM
`configurations` WHERE (`configurations`.`config_key` = 'Locale') LIMIT
1[0m

edit_privilge method in controller is below

def edit_privilege
    @user = User.active.first(:conditions => ["username LIKE
BINARY(?)",params[:id]])
    @employee = @user.employee_record
@privilege_tags=PrivilegeTag.find(:all,:order=>"priority ASC")
    @user_privileges=@user.privileges
if request.post?
      new_privileges = params[:user][:privilege_ids] if params[:user]
      new_privileges ||= []
      @user.privileges = Privilege.find_all_by_id(new_privileges)
      redirect_to :action => 'admission4',:id => @employee.id
    end
  end

Colin

I get the following error when I goto the privilege page.

NoMethodError in Employee#edit_privilege

Showing app/views/employee/edit_privilege.html.erb where line #82
raised:

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
Extracted source (around line #82):

79: <table width="100%" align="center" cellspacing="1"
cellpadding="1" class="table_design">
80: <tr>
81: <% @privilege_tags.each_with_index do
>privilege_tag,ind| %>
82: <% all_privileges =
privilege_tag.privileges.all(:conditions=>"name<>'FinanceControl'",:order=>"priority
ASC").select{|p| p.applicable?} %>

You can get a better handle on this by splitting the above lines into
multiple lines, so something like
<% all_privileges
=privilege_tag.privileges.all(:conditions=>"name<>'FinanceControl'",:order=>"priority
ASC") %>
<% all_privileges = all_privileges.select{|p| p.applicable?} %>

Then you can find which bit is causing the error.

What is p.applicable?

Colin