System.NullReferenceException: 未将对象引用设置到对象的实例。public class Regsit
{
private string username;
private string userpassword;
public Regsit(string username1, string password1)
{
this.username=username1;
this.userpassword = password1;
}
public void Register()
{
Database db = DatabaseFactory.CreateDatabase();
string strSql = "insert into Users(UserName,UserPassword) values('" + username + "','" + userpassword + "')";
DbCommand cmd = db.GetSqlStringCommand(strSql);
try
{
db.ExecuteNonQuery(cmd);
}
catch (Exception ex)
{
throw ex;
}
}
}
Database db = DatabaseFactory.CreateDatabase();这句话 报错 什么原因?
------解决方案--------------------Database db = DatabaseFactory.CreateDatabase();
---->
调试看看DatabaseFactory.CreateDatabase();得到对象了吗?
------解决方案--------------------检查DatabaseFactory.CreateDatabase()方法内部是怎么写的,看相关源文件。
------解决方案--------------------
------解决方案--------------------如果是这一句(Database db = DatabaseFactory.CreateDatabase();)报错,错误的原因只有一个:那就是DatabaseFactory这个对象是空的,请在使用前判断一下。