[英]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:
我沒時間!
這里有一些提示:
嘗試改為在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.