c# - 你调用的对象是空的?

标签 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: 


我没有时间!

最佳答案

尝试在服务器对象上调用 MapPath:

HttpContext.Current.Server.MapPath(FullPath)

关于c# - 你调用的对象是空的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/885268/

相关文章:

java - 将多个字符串保存到一个字符串中

c - 插入哈希表时出现段错误

c++ - fread 在空值处终止中间读取。还读取超过预期数据的垃圾

javascript - 如何使用node.js将大数组写入.txt文件?

c++ - 在 C++ 中实现逐行文件解析器时遇到问题

c# - 如何在 C# 中正确制作 Dllimport?

c# - Entity Framework - 如何不更新相关对象

c# - AWS DynamoDB 使用 QueryAsync 而不是 ScanAsync

c# - 从字符串中分离值

c - 获取二进制文件 C 的哈希值