Important Notice from AspDotNetStorefront
It is with dismay that we report that we have been forced, through the action of hackers, to shut off write-access to this forum. We are keen to leave the wealth of material available to you for research. We have opened a new forum from which our community of users can seek help, support and advice from us and from each other. To post a new question to our community, please visit: http://forums.vortx.com
Results 1 to 5 of 5

Thread: What's wrong with this code?

  1. #1
    mcp111 is offline Junior Member
    Join Date
    Dec 2009
    Posts
    10

    Default What's wrong with this code?

    The bolded line gives error "Object reference not set to an instance of an object"
    Please help!!!

    public static String SafeMapPath(String fname)
    {
    if (string.IsNullOrEmpty(fname) || Path.IsPathRooted(fname))
    {
    return fname;
    }

    string result = fname;

    //Try it as a virtual path. Try to map it based on the Request.MapPath to handle Medium trust level and "~/" paths automatically
    try
    {
    result = HttpContext.Current.Request.MapPath(fname);

    }
    catch
    {
    //Didn't like something about the virtual path.
    //May be a drive path. See if it will expand to a valid path
    try
    {
    //Try a GetFullPath. If the path is not virtual or has other malformed problems
    //Return it as is
    result = Path.GetFullPath(fname);
    }
    catch (NotSupportedException) // Contains a colon, probably already a full path.
    {
    return fname;
    }
    catch (SecurityException exc)//Path is somewhere you're not allowed to access or is otherwise damaged
    {
    throw new SecurityException("If you are running in Medium Trust you may have virtual directories defined that are not accessible at this trust level,\n " + exc.Message);
    }
    }
    return result;
    }

  2. #2
    miguelcrush is offline Senior Member
    Join Date
    Mar 2008
    Posts
    149

    Default

    do you mean to say:

    string result=SafeMapPath(fname);

    because in your code, fname has no value at the point you are try to assign it to result.
    Mike Steyer
    Information Systems Admin

  3. #3
    George the Great is offline Senior Member
    Join Date
    Nov 2006
    Location
    Cleveland, OH
    Posts
    1,792

    Default

    The only way fname wouldn't have a value if is if you passed a null value to the method (fname is the single String parameter passed to it), so I don't think that's the problem.

    What exactly are you trying to do here? That particular line of code is encased in a try/catch so you shouldn't be getting an object reference error on your site. I'll assume you are debugging in Visual Studio. Just breakpoint that line and see which part of it is null. My guess is that you are trying to use SafeMapPath from a custom page somewhere and are doing so before the HttpContext is available or from a page where the HttpContext will never be available, hence the HttpContext is null.
    <a href="http://www.aspdotnetstorefront.com">Shopping Cart Software</a>

  4. #4
    mcp111 is offline Junior Member
    Join Date
    Dec 2009
    Posts
    10

    Smile this is fixed - please ignore!!!

    httpcontext.current was null because I was running the code from a Windows app. Thanks for all your help.

    Partha

  5. #5
    Rob is offline Senior Member
    Join Date
    Aug 2004
    Posts
    3,037

    Default

    ROFL, yea, no web context in a windows app
    AspDotNetStorefront
    Shopping Cart