你调用的对象是空的?

时间:2009-05-19 22:03:45

标签: c# file file-io filesystems

我写了这些行,但我有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: 


我没有时间!

4 个答案:

答案 0 :(得分:2)

以下是一些提示:

  1. 使用Path.Combine从各个部分构建路径
  2. 验证FullPath是否引用了一个文件
    • 出现在磁盘上
    • 网络流程可读(即不在系统帐户下运行网络流程或类似工具)

答案 1 :(得分:2)

尝试在Server对象上调用MapPath:

HttpContext.Current.Server.MapPath(FullPath)

答案 2 :(得分:0)

阅读“Fullpath = TempPath + FileName”,您似乎试图将物理地址作为虚拟地址传递?

是这样的吗?如果不是,你能告诉我们你传入的这个函数的输入吗?如果是物理路径,则不需要使用MapPath。

请参阅here

答案 3 :(得分: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能够满足您的期望。