簡體   English   中英

你調用的對象是空的?

[英]Object reference not set to an instance of an object?

我寫了這些行,但是我有NullReferenceException。 請幫我解決問題!

string FullPath = TempPath + FileName;
System.Drawing.Image Adimg = null;
Adimg = System.Drawing.Image.FromFile(MapPath(FullPath));

我將這些行放在Public bool方法中,並且TempPath是類屬性,而FileName是該方法的輸入。

exception Detail:
System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="System.Web"
  StackTrace:
       at System.Web.UI.Page.MapPath(String virtualPath)
       at FileIO.HasValidAttributes(String FileName) in g:\MyProjects\ASP.net Projects\ADBridge\adengine2\App_Code\FileIO.cs:line 44
       at UploadPage.<Page_Load>b__0(Object sender1, EventArgs e1) in g:\MyProjects\ASP.net Projects\ADBridge\adengine2\UploadPage.aspx.cs:line 29
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 


我沒時間!

這里有一些提示:

  1. 使用Path.Combine從單個片段構建路徑
  2. 驗證FullPath引用的文件是
    • 存在於磁盤上
    • 可被Web進程讀取(即不在系統帳戶或類似帳戶下運行Web進程)

嘗試改為在Server對象上調用MapPath:

HttpContext.Current.Server.MapPath(FullPath)

讀取“ Fullpath = TempPath + FileName”,似乎您正在嘗試將物理地址作為虛擬地址傳遞?

是這樣嗎 如果不是,您能給我們提供您作為函數輸入的內容嗎? 如果這是物理路徑,則無需使用MapPath。

這里

嘗試這個:

string fullPath = Path.Combine(TempPath, FileName);
System.Drawing.Image adimg = null;
if (!String.IsNullOrEmpty(fullPath))
{
    string serverPath = HttpContext.Current.Server.MapPath(fullPath);
    if (!String.IsNullOrEmpty(serverPath))
        adimg = System.Drawing.Image.FromFile(serverPath);
}

確保MapPath能夠滿足您的期望。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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