【发布时间】:2014-05-19 18:45:48
【问题描述】:
我试图在我的页面上的标签中显示我创建的类的属性Employee- 该属性是description。
Employee 类的变量从数据库中获取它的值。
这里是类:
public class Employee : User
{
public string Gender;
public string DateOfBirth;
public string EducationYears;
public string Field;
public string SubField;
public string WorkAt;
public string description;
public string resume;
public string word;
public Employee() : base() { }
public Employee(string Gender,string DateOfBirth, string EducationYears, string Field, string SubField, string WorkAt, string description, string resume, string word,
string email, int status, string name, string photo)
:base(email, status, name, photo)
{
this.Gender=Gender;
this.DateOfBirth=DateOfBirth;
this.EducationYears=EducationYears;
this.Field=Field;
this.SubField=SubField;
this.WorkAt=WorkAt;
this.description = description;
this.resume = resume;
this.word = word;
}
public string getDescription()
{
return this.description;
}
}
这里是用户:
public class User
{
public string email;
public int status;
public string name;
public string photo;
public User() { }
public User(string email, int status, string name, string photo)
{
this.email = email;
this.status = status;
this.name = name;
this.photo = photo;
}
}
这是我的 asp.net:
public Employee ee = new Employee();
protected void Page_Load(object sender, EventArgs e)
{
User = "xxx";
ee = getEmployee(User);
}
这里是getEmployee(string User):
public Employee getEmployee(string email)
{
DataTableReader r = DAL.getReader("SELECT * FROM Users WHERE Email='" + email + "'");
while (r.Read())
{
DataTableReader ee = DAL.getReader("SELECT * FROM Users WHERE Email='" + email + "'");
while (r.Read())
{
return (new Employee(r["Gender"].ToString(), r["DateOfBirth"].ToString(), r["EducationYears"].ToString(), r["field"].ToString(), r["subField"].ToString(), r["WorkAT"].ToString(), r["description"].ToString(), r["Resume"].ToString(), r["word"].ToString(), r["Email"].ToString(), int.Parse(ee["Status"].ToString()), r["Fname"].ToString() + r["Lname"].ToString(), ee["photo"].ToString()));
}
}
return null;
}
我的html:
<asp:Label ID="Label1" runat="server" Text='<%=ee.description %>'></asp:Label>
我运行调试器,它停在 html 行 ^ 并给了我错误:
System.NullReferenceException: Object reference not set to an instance of an object.
当我尝试这样做时:
public Employee ee = new Employee();
protected void Page_Load(object sender, EventArgs e)
{
User = "xxx";
ee.description="something";
}
调试器停在ee.description="something"; 行并给出了同样的错误提示。
有什么问题,我该如何解决?感谢您的帮助
【问题讨论】:
-
我假设
getEmployee返回null。我必须承认我不知道你在那里做什么。为什么不简单地使用DataAdapter来填充DataTable,或者只使用单个while而不是嵌套的? -
首先,您的
description是成员字段,而不是属性。其次 - 你为什么要制定一种获得 PUBLIC 成员的方法?第三:如果您要为其分配方法结果,则构造public Employee ee = new Employee();是没有意义的。第四:重复的 NullReferenceException 问题。 stackoverflow.com/q/4660142/1284902 -
为什么它们都是字符串,这会给你不应该出现的问题带来重大问题。公共成员变量也只是要求提供错误目录。
-
谢谢大家,帮助的 Tarec。我想我还有很多东西要学