簡體   English   中英

如何在MVC Web服務中修復此“ system.nullreferenceexception對象引用未設置為對象實例”

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM