繁体   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