views:

805

answers:

4

I wrote these lines but i have NullReferenceException. please help me how to fix it!

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

I put these lines in a Public bool method and TempPath is class property and FileName is input for the method.

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:


I have no Time!

+2  A: 

Here's a few tips:

  1. Use Path.Combine to build paths from individual pieces
  2. Verify that FullPath refer to a file that is
    • Present on disk
    • Readable by the web process (ie. not running the web process under System Account or similar)
Lasse V. Karlsen
string FullPath = Path.Combine(TempPath, FileName);but is not working. the problem is still exists! notice my file is Peresent on disk.
mahdiahmadirad
string FullPath = Path.Combine(TempPath, FileName);but is not working. the problem is still exists! notice my file is Peresent on disk.
mahdiahmadirad
Did you check that other thing, that the web service user has access to the file?
Lasse V. Karlsen
A: 

Reading the "Fullpath = TempPath + FileName", it seems that you are trying to pass a physical address in as a virtual address?

Is this the case? Can you give us what you are passing in as input to this function if it's not? If it is the physical path, there shouldn't be a need to use MapPath.

See here

Fry
my TempPath: private string _TempPath = "~/Images/"; public string TempPath { get { return _TempPath; } set { _TempPath = value; } }my file name: "imdb.png"
mahdiahmadirad
A: 

Try calling MapPath on the Server object instead:

HttpContext.Current.Server.MapPath(FullPath)
Adam Ruth
A: 

Try this:

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);
}

Make sure that the MapPath is giving you what you're expecting.

Alexander Kahoun