system.drawing error. Need help debugging.

Discussion in 'ASP .Net' started by darrel, Feb 22, 2006.

  1. darrel

    darrel Guest

    I'm modyfying an image upload and resizing script that we've had laying
    around for a long while.

    I'm getting a NullReferenceException error (see full error at bottom) from
    this line:


    Dim g As System.Drawing.Image =
    System.Drawing.Image.FromFile(strTempFullFileName)

    I'm not sure what I should be checking for based on that error. There is a
    file located where 'strTempFullFileName' points to.

    Any suggestions on what else to check for would be appreciated.

    -Darrel

    FULL ERROR:

    [NullReferenceException: Object reference not set to an instance of an
    object.]
    imageUpload.ImageUpload.btnUpload_Click(Object sender, EventArgs e) in
    C:\Inetpub\wwwroot\CourtsCMS\imageUpload\ImageUpload.aspx.vb:552
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
    eventArgument) +57
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
    sourceControl, String eventArgument) +18
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
    System.Web.UI.Page.ProcessRequestMain() +1292
     
    darrel, Feb 22, 2006
    #1
    1. Advertising

  2. Darrel,

    Did you set a breakpoint at this line to make sure that strTempFullFileName
    is not null?

    Eliyahu

    "darrel" <> wrote in message
    news:...
    > I'm modyfying an image upload and resizing script that we've had laying
    > around for a long while.
    >
    > I'm getting a NullReferenceException error (see full error at bottom) from
    > this line:
    >
    >
    > Dim g As System.Drawing.Image =
    > System.Drawing.Image.FromFile(strTempFullFileName)
    >
    > I'm not sure what I should be checking for based on that error. There is a
    > file located where 'strTempFullFileName' points to.
    >
    > Any suggestions on what else to check for would be appreciated.
    >
    > -Darrel
    >
    > FULL ERROR:
    >
    > [NullReferenceException: Object reference not set to an instance of an
    > object.]
    > imageUpload.ImageUpload.btnUpload_Click(Object sender, EventArgs e) in
    > C:\Inetpub\wwwroot\CourtsCMS\imageUpload\ImageUpload.aspx.vb:552
    > System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
    >
    > System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
    > eventArgument) +57
    > System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
    > sourceControl, String eventArgument) +18
    > System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
    > System.Web.UI.Page.ProcessRequestMain() +1292
    >
     
    Eliyahu Goldin, Feb 22, 2006
    #2
    1. Advertising

  3. darrel

    Darren Kopp Guest

    Without seeing your code, you will need to Upload the file, save it
    someplace, then load from where you saved the image. Also try this:

    Dim g as New System.Drawing.Image
    g = System.Drawing.Image.FromFile(strTempFullFileName)

    Could you post the code on where you are getting strTempFullFileName
    from? Feel free to check out
    http://www.easerve.com/developer/tutorials/asp-net-tutorials-upload-image-file.aspx.
    That article has a decent tutorial of how to upload an image with
    vb.net

    HTH,
    Darren Kopp
    http://blog.secudocs.com/
     
    Darren Kopp, Feb 22, 2006
    #3
  4. darrel

    darrel Guest


    > Did you set a breakpoint at this line to make sure that
    > strTempFullFileName
    > is not null?


    Yep. The error comes from this specific line, and strTempFullFileName does
    resolve to a full file path that does point to the specific file.

    -Darrel
     
    darrel, Feb 22, 2006
    #4
  5. darrel

    darrel Guest

    > Without seeing your code, you will need to Upload the file, save it
    > someplace, then load from where you saved the image.


    Right. That's what this line is doing, grabbing the file that I've already
    uploaded. The STR does resolve to a full path that does point at the file:

    D:\Web\Applications\assets\images\_temp\imageUploadTest.jpg

    But maybe that path is malformed? Does it look correct?

    > Dim g as New System.Drawing.Image
    > g = System.Drawing.Image.FromFile(strTempFullFileName)


    I tried that, but get an error in the IDE:

    Dim g As New System.Drawing.Image

    ('new' cannot be used on a class that is declared 'MustInherit')

    -Darrel
     
    darrel, Feb 22, 2006
    #5
  6. darrel

    Darren Kopp Guest

    Ah, here we go. Image is an abstract class. try this

    Dim g as New System.Drawing.Bitmap(strTempFullFileName)

    Don't worry that it's a "Bitmap", that just means it deals with pixels.
    You can call something like g.save("bla.jpg", ImageFormat.Jpeg) to
    save jpg's and such.

    HTH,
    Darren Kopp
    http://blog.secudocs.com/
     
    Darren Kopp, Feb 22, 2006
    #6
  7. darrel

    darrel Guest


    > Dim g as New System.Drawing.Bitmap(strTempFullFileName)


    I was optimitic! The IDE does let me enter that, but, alas, I get the exact
    same error when I try to execute it.

    -Darrel
     
    darrel, Feb 22, 2006
    #7
  8. darrel

    Darren Kopp Guest

    could you post the code for how you upload the image and where you
    assign a value to strTempFullFileName? The more code to look at the
    easier it will be to find out what's happening.

    -Darren Kopp
    http://blog.secudocs.com/
     
    Darren Kopp, Feb 22, 2006
    #8
  9. darrel

    darrel Guest


    > could you post the code for how you upload the image and where you
    > assign a value to strTempFullFileName? The more code to look at the
    > easier it will be to find out what's happening.


    Sure, though, that part of the code does execute just fine. The image is
    uploaded and is sitting in that particular folder:

    ----------------------------------------------------------------------
    Dim postedFile = uploadedFile.PostedFile
    Dim contentType As String = postedFile.ContentType
    Dim contentLength As Integer = postedFile.ContentLength

    strUploadFileName = Mid(postedFile.FileName, InStrRev(postedFile.FileName,
    "\") + 1)

    'clean up the filename
    strUploadFileName = fixFileName(strUploadFileName)

    'Temp file name/location
    strTempFullFileName = savePath & "_temp\" & strUploadFileName

    'save the temp file
    postedFile.SaveAs(strTempFullFileName)

    resizeImage(categoryFolder, customImageSize, "jpg", strUploadFileName)

    ----------------------------------------------------------------------

    The file I am trying to retrieve does have the correct path and file name as
    the string is resolving to.

    -Darrel
     
    darrel, Feb 22, 2006
    #9
  10. darrel

    Darren Kopp Guest

    Where do you declare strTempFullFileName? I don't see Dim
    strTempFullFileName as String. That could be the cause of the
    exception, but even with that i wouldn't think it would compile. Also
    shouldn't it be...

    Dim postedFile as HttpPostedFile = uploadedFile.PostedFile?

    -Darren Kopp
     
    Darren Kopp, Feb 22, 2006
    #10
  11. darrel

    darrel Guest

    > Where do you declare strTempFullFileName? I don't see Dim
    > strTempFullFileName as String. That could be the cause of the
    > exception, but even with that i wouldn't think it would compile. Also
    > shouldn't it be...


    Darren:

    Thanks for all the help. In the process of trying to send over the code, I
    gave it yet another once over.

    I think I discovered a user error on my part. One function was erroring out,
    cascading back to the parent function, which then tried to execute a CAPTCH
    statement, which, itself, had an error.

    So, all this time, the error I was receiving wasn't referring to the actual
    problem.

    ARGH!

    ;o)

    -Darrel
     
    darrel, Feb 23, 2006
    #11
  12. darrel

    Darren Kopp Guest

    Hahaha, yea... I have felt the sting of those mistakes before too.
    Glad to hear that you resolved the problem.

    Happy .NETing,
    Darren Kopp
    http://blog.secudocs.com/
     
    Darren Kopp, Feb 23, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carlos

    Error using system.drawing

    Carlos, Feb 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    405
    Kevin Spencer
    Feb 4, 2004
  2. Richard
    Replies:
    3
    Views:
    4,150
    velocity4anil
    Sep 19, 2007
  3. Replies:
    1
    Views:
    665
    Kevin Spencer
    Jan 9, 2006
  4. byrd48
    Replies:
    0
    Views:
    3,588
    byrd48
    Jul 6, 2006
  5. defn noob
    Replies:
    1
    Views:
    416
    Mark Space
    Jun 28, 2008
Loading...

Share This Page