打印

在抄样例运行的时候出现了一个错误-nil.to_sym,大家帮帮忙

在抄样例运行的时候出现了一个错误-nil.to_sym,大家帮帮忙

是一个连接数据库的程序,接照例子的顺序来做的,别的都可以实现,就是这一步未能实现。在运行的时候出现了这样的错误,"You have a nil object when you didn't expect it! The error occurred while evaluating nil.to_sym"不知道这是什么错误啊,大家看看这个问题是出于什么原因,代码我就不用贴了,个人觉得,贴了代码,会有很多人就会“我认为。。。”或者是,“可能是。。。”,再就是“把**地方改一下再试一下”,我不想要这样的答案,有遇到过这种问题的朋友看看这是什么情况引起这种异常的就行了。小弟在这里谢谢了。

TOP

你期待的某个对象为nil,所以在调用这个对象的to_sym方法时出错了

至于具体原因,不把详细的错误信息给出来,很难判断的

可以不贴代码,无妨贴一下debug信息?
First they ignore you, then they laugh at you, then they fight you, then you win.

TOP

同感

我是在测试的时候遇到了这样的情况,NoMethodError:You have a nil object when you didn't expect it!
The error occured while evaluating nil.search_name
就是这样的错误,请帮忙看以下,谢谢了!

TOP

估计碰到这个问题的人都是英语不好的……

出现这个异常的时候,错误信息里面包括错误的行号,文件名,在源代码里面找到这一行,存在一个对象的方法调用,比如 some_string.to_sym 或者 another_object.search_name。因为该对象(例子 some_string 和 another_object)为 nil 才会产生这样的错误。
我就是鸡蛋黄……

TOP

还不是明白

即使是因为英语差的原因,但是,你说的这个因为该对象(例子 some_string 和 another_object)为 nil 才会产生这样的错误。another-object是什么,能给我说以下吗?我的是测试时出现的问题!我把的代给你,你看下,那地方出错了!
def test_search_by_name
    #member=Member.basic_info(search_name)
    get :search_by_name,:search_name=>"李莞"
    #assert_valid assigns(:basic_info)
    assert_equal basic_infos(:liwan_basic_info).true_name,assigns(:member_basic_info).search_name
    assert_response :success
    #assert_template '_search_university_classmate'
  end
运行这个的时候就出现我上面的问题!被测试的方法是:
def search_by_name
    @members = Member.search_members_by_name(params[:search_name],params[:page])
    @totalrecords=@members.total_entries
    @start=(@members.current_page-1)*10+1
    if (@totalrecords /10).floor+1==@members.current_page
       @end=@members.total_entries
     else
       @end=@members.current_page*10
    end
     render :action=>"search"
  end
我觉得我的对象并不是为空的!

TOP

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

这个问题我已经解决了,不过还是要谢谢楼上的兄弟给的提示!

TOP