Forum: Ruby on Rails What is this error

1d4d78bf072607d3522fe3ceae4b7a9e?d=identicon&s=25 keerthi priya (Guest)
on 2013-08-19 20:56
(Received via mailing list)
Hi all,

I am getting this bellow error

This is my code(method):
def self.generate_normal_report(batch_ids)
    batches = Batch.find(:all,:conditions => {:id => batch_ids},:select
=>
"id")
    batches.each do |batch|
      exam_groups_main = batch.exam_groups
     exam_scores_main =
exam_groups_main.map(&:exams).flatten.compact.map(&:exam_scores).flatten.compact
    archived_exam_scores_main =
exam_groups_main.map(&:exams).flatten.compact.map(&:archived_exam_scores).flatten.compact
      terms = batch.terms.map{|term| term.id.to_s}
    students = batch.students.all(:select =>
"id,first_name,batch_id,user_id")
    student_ids = students.map(&:id)
    archived_students = ArchivedStudent.all(:conditions => {:batch_id =>
batch.id},:select => "id,first_name,batch_id,former_id")
      student_ids.each do |student_id|
        student_report = Hash.new
        student = Student.find(student_id)
    student_report[:term_report] = Hash.new
        term_report = Hash.new
        terms.each do |term|
          exam_group_report = Hash.new
          exam_groups = exam_groups_main.select{|exam_group|
exam_group.term_id.to_s == term.to_s }
          exam_groups.each do |exam_group|
            exam_group_report[exam_group.id.to_s.to_sym] = Hash.new
            exam_group_report[exam_group.id.to_s.to_sym][:exam_report] =
Array.new
            exams = exam_group.exams
            exam_report = Hash.new
            exams.each do |exam|
              student_exam_score = exam_scores_main.select{|score|
score.exam_id == exam.id and score.student_id == student.id}.first
              student_mark = student_exam_score.nil? ? 0
:student_exam_score.marks
          all_archived_score = archived_exam_scores_main.select{|score|
score.exam_id == exam.id}.flatten.compact.map(&:marks)
              all_normal_score = exam_scores_main.select{|score|
score.exam_id == exam.id }.flatten.compact.map(&:marks)
              all_score = (all_archived_score +
all_normal_score).compact.sort.reverse
              exam_position = all_score.index(student_mark).nil? ?
all_score.size : (all_score.index(student_mark) + 1)
              exam_report = {:subject_id => exam.subject.id,:exam_id =>
exam.id,:marks => student_mark,:maximum_marks =>
exam.maximum_marks,:exam_position => exam_position}
              if all_score.size == 0
                exam_report[:exam_average] = 0
              else
                exam_report[:exam_average] = (all_score.sum.to_f /
all_score.size)
              end
              exam_group_report[exam_group.id.to_s.to_sym][:exam_report]
<<
exam_report
            end
            class_information =
ExamReport.calculate_class_position_and_class_average(exam_group,student,students,archived_students)

exam_group_report[exam_group.id.to_s.to_sym][:exam_group_position] =
class_information[:rank]

exam_group_report[exam_group.id.to_s.to_sym][:exam_group_average]  =
class_information[:class_average]

exam_group_report[exam_group.id.to_s.to_sym][:exam_group_total]  =
class_information[:student_mark]
          end
          term_report[term.to_sym] = Hash.new
          term_information =
ExamReport.calculate_term_position_and_term_average(term,exam_groups,student,students,archived_students)
      term_report[term.to_sym][:exam_group_report] = exam_group_report
          term_report[term.to_sym][:term_position] =
term_information[:rank]
          term_report[term.to_sym][:term_average] =
term_information[:class_average]
          term_report[term.to_sym][:term_total] =
term_information[:student_mark]
        end
        student_report[:term_report] = term_report
        annual_information =
ExamReport.calculate_annual_position_and_annual_average(student,students,archived_students,batch.terms)
    student_report[:annual_position] = annual_information[:rank]
        student_report[:annual_average] =
annual_information[:class_average]
        student_report[:annual_total] =
annual_information[:student_mark]
        exam_report = ExamReport.find_by_student_id(student_id)
        full_report = Hash.new
        full_report[:batch_id] = student.batch_id
        full_report[:report] = student_report
        report_array = Array.new
        report_array << full_report
        if student.graduated_batches.empty?
          if exam_report.nil?
            report_card = ExamReport.new(:student_id =>
student.id,:report_card => report_array)
        report_card.save
            report_card.report_card.select{|r| r[:batch_id] ==
student.batch.id}.first[:combined_report] = Hash.new
            report_card.report_card.select{|r| r[:batch_id] ==
student.batch.id}.first[:combined_report] =
self.prepare_combined_marksheet(student,batch_ids,student_report)
            report_card.report_card.select{|r| r[:batch_id] ==
student.batch.id}.first[:batch_combined_report] = Hash.new
            report_card.report_card.select{|r| r[:batch_id] ==
student.batch.id}.first[:batch_combined_report] =
self.prepare_combined_marksheet(student,student.batch.id.to_a,student_report)
            report_card.save
          else
            exam_report.destroy
            report_card = ExamReport.new(:student_id =>
student.id,:report_card => report_array)
            report_card.save
            report_card.report_card.select{|r| r[:batch_id] ==
student.batch.id}.first[:combined_report] = Hash.new
            report_card.report_card.select{|r| r[:batch_id] ==
student.batch.id}.first[:combined_report] =
self.prepare_combined_marksheet(student,batch_ids,student_report)
            report_card.report_card.select{|r| r[:batch_id] ==
student.batch.id}.first[:batch_combined_report] = Hash.new
            report_card.report_card.select{|r| r[:batch_id] ==
student.batch.id}.first[:batch_combined_report] =
self.prepare_combined_marksheet(student,student.batch.id.to_a,student_report)
            report_card.save
          end
        else
          unless exam_report.nil?
            if exam_report.report_card.select{|report| report[:batch_id]
==
student.batch.id}.empty?

              exam_report.report_card << full_report
              exam_report.save  ///HERE I AM UNABLE TO SAVE IT

              exam_report.report_card.select{|r| r[:batch_id] ==
student.batch.id}.first[:combined_report] = Hash.new
              exam_report.report_card.select{|r| r[:batch_id] ==
student.batch.id}.first[:combined_report] =
self.prepare_combined_marksheet(student,batch_ids,student_report)
              exam_report.report_card.select{|r| r[:batch_id] ==
student.batch.id}.first[:batch_combined_report] = Hash.new
              exam_report.report_card.select{|r| r[:batch_id] ==
student.batch.id}.first[:batch_combined_report] =
self.prepare_combined_marksheet(student,student.batch.id.to_a,student_report)
              exam_report.save
       else
              exam_report.report_card.delete_if{|report|
report[:batch_id]
== student.batch.id}
              exam_report.report_card << full_report
              exam_report.save
              exam_report.report_card.select{|r| r[:batch_id] ==
student.batch.id}.first[:combined_report] = Hash.new
              exam_report.report_card.select{|r| r[:batch_id] ==
student.batch.id}.first[:combined_report] =
self.prepare_combined_marksheet(student,batch_ids,student_report)
              exam_report.report_card.select{|r| r[:batch_id] ==
student.batch.id}.first[:batch_combined_report] = Hash.new
              exam_report.report_card.select{|r| r[:batch_id] ==
student.batch.id}.first[:batch_combined_report] =
self.prepare_combined_marksheet(student,student.batch.id.to_a,student_report)
              exam_report.save
            end
          else
            report_card = ExamReport.new(:student_id =>
student.id,:report_card => report_array)
          end
        end
      end
    end
  end

 No method error undefined method `each' for nil:NilClass

NoMethodError (undefined method `each' for nil:NilClass):
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:40:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:39:in
`map'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:39:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in `call'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in `emit'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in
`quick_emit'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:38:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:18:in
`node_export'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:18:in
`add'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:18:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:17:in
`each'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:17:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:16:in
`map'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:16:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in `call'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in `emit'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in
`quick_emit'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:15:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:18:in
`node_export'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:18:in
`add'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:18:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:17:in
`each'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:17:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:16:in
`map'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:16:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in `call'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in `emit'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in
`quick_emit'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:15:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:41:in
`node_export'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:41:in
`add'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:41:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:40:in
`each'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:40:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:39:in
`map'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:39:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in `call'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in `emit'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in
`quick_emit'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:38:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:110:in
`node_export'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:110:in
`add'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:110:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:109:in
`each'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:109:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:108:in
`seq'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:108:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in `call'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in `emit'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in
`quick_emit'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:107:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:41:in
`node_export'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:41:in
`add'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:41:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:40:in
`each'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:40:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:39:in
`map'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:39:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in `call'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in `emit'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in
`quick_emit'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:38:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:41:in
`node_export'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:41:in
`add'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:41:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:40:in
`each'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:40:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:39:in
`map'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:39:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in `call'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in `emit'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in
`quick_emit'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:38:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:110:in
`node_export'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:110:in
`add'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:110:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:109:in
`each'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:109:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:108:in
`seq'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:108:in
`to_yaml'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in `call'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in `emit'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml.rb:391:in
`quick_emit'
  /opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/yaml/rubytypes.rb:107:in
`to_yaml'
  app/models/exam_report.rb:89:in `generate_normal_report'
  app/models/exam_report.rb:15:in `each'
  app/models/exam_report.rb:15:in `generate_normal_report'
  app/models/exam_report.rb:7:in `each'
  app/models/exam_report.rb:7:in `generate_normal_report'
  app/controllers/exam_reports_controller.rb:73:in
`normal_report_generation'
  passenger (3.0.9) lib/phusion_passenger/rack/request_handler.rb:96:in
`process_request'
  passenger (3.0.9)
lib/phusion_passenger/abstract_request_handler.rb:513:in
`accept_and_process_next_request'
  passenger (3.0.9)
lib/phusion_passenger/abstract_request_handler.rb:274:in `main_loop'
  passenger (3.0.9)
lib/phusion_passenger/classic_rails/application_spawner.rb:321:in
`start_request_handler'
  passenger (3.0.9)
lib/phusion_passenger/classic_rails/application_spawner.rb:275:in `send'
  passenger (3.0.9)
lib/phusion_passenger/classic_rails/application_spawner.rb:275:in
`handle_spawn_application'
  passenger (3.0.9) lib/phusion_passenger/utils.rb:479:in `safe_fork'
  passenger (3.0.9)
lib/phusion_passenger/classic_rails/application_spawner.rb:270:in
`handle_spawn_application'
  passenger (3.0.9) lib/phusion_passenger/abstract_server.rb:357:in
`__send__'
  passenger (3.0.9) lib/phusion_passenger/abstract_server.rb:357:in
`server_main_loop'
  passenger (3.0.9) lib/phusion_passenger/abstract_server.rb:206:in
`start_synchronously'
  passenger (3.0.9) lib/phusion_passenger/abstract_server.rb:180:in
`start'
  passenger (3.0.9)
lib/phusion_passenger/classic_rails/application_spawner.rb:149:in
`start'
  passenger (3.0.9) lib/phusion_passenger/spawn_manager.rb:219:in
`spawn_rails_application'
  passenger (3.0.9)
lib/phusion_passenger/abstract_server_collection.rb:132:in
`lookup_or_add'
  passenger (3.0.9) lib/phusion_passenger/spawn_manager.rb:214:in
`spawn_rails_application'
  passenger (3.0.9)
lib/phusion_passenger/abstract_server_collection.rb:82:in `synchronize'
  passenger (3.0.9)
lib/phusion_passenger/abstract_server_collection.rb:79:in `synchronize'
  passenger (3.0.9) lib/phusion_passenger/spawn_manager.rb:213:in
`spawn_rails_application'
  passenger (3.0.9) lib/phusion_passenger/spawn_manager.rb:132:in
`spawn_application'
  passenger (3.0.9) lib/phusion_passenger/spawn_manager.rb:275:in
`handle_spawn_application'
  passenger (3.0.9) lib/phusion_passenger/abstract_server.rb:357:in
`__send__'
  passenger (3.0.9) lib/phusion_passenger/abstract_server.rb:357:in
`server_main_loop'
  passenger (3.0.9) lib/phusion_passenger/abstract_server.rb:206:in
`start_synchronously'
  passenger (3.0.9) helper-scripts/passenger-spawn-server:99
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2013-08-19 21:27
keerthi priya wrote in post #1119115:
> Hi all,
>
> I am getting this bellow error

>  No method error undefined method `each' for nil:NilClass

The error is what it says it is. You are sending the message 'each" to a
nil object reference. Look back through the stack trace until you get to
the first line containing your own code...

> NoMethodError (undefined method `each' for nil:NilClass):
>
> ... a bunch of framework references....
>

Something you should recognize...
>   app/models/exam_report.rb:89:in `generate_normal_report'

Line 89 of exam_report.rb. Find out which object is receiving the 'each'
message and figure out why the reference is nil.

>
> ... stack trace continues ...
>
Db9510c96a4c0f6f88c9aa39ab24211a?d=identicon&s=25 jsnark (Guest)
on 2013-08-21 15:27
(Received via mailing list)
This code needs to be refactored.  It is way too long.  Many lines are
too
long.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.