![](/img/trans.png)
[英]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.