[英]How to fix System.NullReferenceException: Object reference not set to an instance of an object
[英]How to Fix This “system.nullreferenceexception object reference not set to an instance of an object” in mvc web service
在我的控制器中
public string Post(Order od)
{
string result = "";
Database newDB= new Database("New");
try
{
newDB.Execute(@"insert into TABLE_A(BranchID,CustomerName,BirthDate,BirthPlace)
Select @0,@1,@2,@3",
new object[] {od.BranchID,od.CustomerName,od.BirthDate,od.BirthPlace});
newDB.CloseSharedConnection();
result = "succes";
}
catch (Exception Ex)
{
result = Ex.Message;
}
return result;
}
错误信息
System.NullReferenceException:对象引用未设置为对象的实例”
我尝试创建一个新的对象odr:
public string Post(Order od)
{
string result = "";
Order odr = new Order();
Database newDB= new Database("New");
try
{
Log.Info("REQ:" + od.BranchID + "|" + od.CustomerName + "|" + od.BirthDate + "|" + od.BirthPlace);
newDB.Execute(@"insert into TABLE_A(BranchID,CustomerName,BirthDate,BirthPlace)
Select @0,@1,@2,@3",
new object[] {odr.BranchID,odr.CustomerName,odr.BirthDate,odr.BirthPlace});
newDB.CloseSharedConnection();
result = "succes";
}
catch (Exception Ex)
{
result = Ex.Message;
Log.Error(Ex)
}
return result;
}
但是我发布的数据为空。 请帮助我的问题:(
NullRefException是您应该学习并避免的基本编程错误之一。 您是如何设置collectionDB的? 或者,如果不是collectionDB为null,请在此处共享异常stacktrace,以便我们看到并帮助您指出造成这种情况的原因。
此外,不要像这样捕获顶级异常。 在所有情况下,不记录日志就捕获异常是一种不好的做法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.