我认为我的问题可能是由于对Find()函数的误用/误解,所以这可能不是空引用一般线程的完全重复问题。
我试图在checkTime变量的条件下为每个学生提出警报。 我在for循环中得到一个空引用。
def check_cupom
@cupom = Coupon.find_by(name: params[:cupom])
if @cupom.blank?
render :text => "record_not_found"
else
discount = @cupom.price.gsub(/[^\d^\.]/, '')
render :text => discount
end
end
这是我的型号代码: 额外细节: 这是从Web获得的一些示例代码,一般的想法是尝试将其转换为项目管理程序,我可以在dbSet中添加和删除对象,并指定这些对象的到期时间。
这是我尝试通过遍历所有对象来访问每个单个对象的checkTime值,然后尝试为每个符合到期条件的对象抛出一个allert。
ClientScriptManager cs = Page.ClientScript;
Student[] myArr = new Student[db.Students.Count()];//this line is fine
Student set = new Student();
string[] myStringVariable = new string[db.Students.Count()];
for (int i=0;i<db.Students.Count();i++)
{
set = db.Students.Find(i);
myStringVariable[i] = "times up: " + set.checkTime; //System.NullReferenceException: 'Object reference not set to an instance of an object.'
myArr[i]= db.Students.Find(i);
// if (DateTime.Now >= myArr[i].checkTime)//err:object reference not set to instance of an Object
//{
//}
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable[i] + "');", true);
}