2009-05-19 90 views
2

我寫了這些行,但我有NullReferenceException。請幫助我如何解決它!
未將對象引用設置爲對象的實例?

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

我把這些線在公共布爾方法和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: 


我沒有時間!

+0

所以 - 你的Temp路徑和你的文件名是什麼?從堆棧跟蹤看,問題出在哪裏 – Fry 2009-05-19 22:30:29

回答

2

嘗試調用MapPath的服務器對象上,而不是:

HttpContext.Current.Server.MapPath(FullPath) 
2

下面是一些提示:

  1. 使用Path.Combine從各個部分構建路徑
  2. 驗證FULLPATH是指一個文件,該文件是
    • 目前上盤
    • 可讀web進程(即不運行系統帳戶下的web進程或類似進程)
+0

string FullPath = Path.Combine(TempPath,FileName); 但不起作用。問題依然存在!注意我的文件在磁盤上是Peresent。 – mahdiahmadirad 2009-05-19 22:25:03

+0

string FullPath = Path.Combine(TempPath,FileName); 但不起作用。問題依然存在!注意我的文件在磁盤上是Peresent。 – mahdiahmadirad 2009-05-19 22:25:09

0

讀取「Fullpath = TempPath + FileName」,看起來您正嘗試將物理地址作爲虛擬地址傳遞?

這是這種情況?如果不是,你可以給我們輸入這個函數嗎?如果是物理路徑,則不需要使用MapPath。

here

0

試試這個:

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是給你你期待什麼。

相關問題