Object variable or With block variable not set

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neo Geshel

    #1

    Object variable or With block variable not set

    I am trying to deal with an image in code-behind. I consistently get the
    following error:

    Server Error in '/' Application.
    Object variable or With block variable not set.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.NullRefe renceException: Object variable or
    With block variable not set.

    Source Error:

    An unhandled exception was generated during the execution of the current
    web request. Information regarding the origin and location of the
    exception can be identified using the exception stack trace below.

    Stack Trace:

    [NullReferenceEx ception: Object variable or With block variable not set.]
    Microsoft.Visua lBasic.Compiler Services.LateBi nding.LateGet(O bject o,
    Type objType, String name, Object[] args, String[] paramnames, Boolean[]
    CopyBack) +934
    TeamMetz.Add30_ Preview(Object sender, EventArgs e) +73
    System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e) +108

    System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring
    eventArgument) +57
    System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler
    sourceControl, String eventArgument) +18
    System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData) +33
    System.Web.UI.P age.ProcessRequ estMain() +1292


    Version Information: Microsoft .NET Framework Version:1.1.432 2.2300;
    ASP.NET Version:1.1.432 2.2300





    What is happening is that I try to catch the image using the following:

    Private Sub Add30_Preview(B yVal sender As Object, ByVal e As EventArgs)
    Dim imgContent as Object = request.form("I mageUpLoad")
    Dim imgStream as Stream
    imgStream = imgContent.Post edFile.InputStr eam


    It is clear that the error occurs at the last two lines, because if I
    comment them out, everything goes fine (except that the images cannot be
    "handled", and therefore do not come up in the preview).

    TIA.
    ...Geshel
    --
    *************** *************** *************** *************** ***********
    * My reply-to is an automatically monitored spam honeypot. Do not use *
    * it unless you want to be blacklisted by SpamCop. Please reply to my *
    * first name at my last name dot org. *
    *************** *************** *************** *************** ***********
    * “I contend that we are both atheists. I just believe in one fewer *
    * god than you do. When you understand why you dismiss all the other *
    * possible gods, you will understand why I dismiss yours.” *
    * - Stephen F. Roberts *
    *************** *************** *************** *************** ***********
    * “Anyone who believes in Intelligent Design (“creationism ”) is just *
    * as ignorant, irrational and ill-educated as someone who believes *
    * that the world is a flat disc, that the Sun circles the Earth or *
    * that there really is a tooth fairy. Darwinism has an overwhelming *
    * foundation of evidence that can be tested and reproduced. *
    * *
    * “Intelligent Design, on the other hand, has no evidence at all;not *
    * one single shred of testable proof. As such, Intelligent Design is *
    * Religious Mythology, and has no right whatsoever to be in our *
    * Science classrooms.” - 99.99+% of Scientists *
    *************** *************** *************** *************** ***********
    Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
    obsessed with sex as the average man.” Unfortunately, since true
    nymphomaniacs are so rare, this means that it takes an extraordinary
    woman to keep up with an ordinary man.
    *************** *************** *************** *************** ***********
  • Cor Ligthert [MVP]

    #2
    Re: Object variable or With block variable not set

    Neo,

    This is a class where is written "must inherit" so it cannot used as this,
    beside that you have to instance a class as new. Would you not have to
    overlook your code what was the base for this.

    Cor

    "Neo Geshel" <gotcha@geshel. org> schreef in bericht
    news:eiIwVRTTGH A.424@TK2MSFTNG P12.phx.gbl...
    I am trying to deal with an image in code-behind. I consistently get the
    following error:

    Server Error in '/' Application.
    Object variable or With block variable not set.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.NullRefe renceException: Object variable or
    With block variable not set.

    Source Error:

    An unhandled exception was generated during the execution of the current
    web request. Information regarding the origin and location of the
    exception can be identified using the exception stack trace below.

    Stack Trace:

    [NullReferenceEx ception: Object variable or With block variable not set.]
    Microsoft.Visua lBasic.Compiler Services.LateBi nding.LateGet(O bject o,
    Type objType, String name, Object[] args, String[] paramnames, Boolean[]
    CopyBack) +934
    TeamMetz.Add30_ Preview(Object sender, EventArgs e) +73
    System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e) +108

    System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring
    eventArgument) +57
    System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler
    sourceControl, String eventArgument) +18
    System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData) +33
    System.Web.UI.P age.ProcessRequ estMain() +1292


    Version Information: Microsoft .NET Framework Version:1.1.432 2.2300;
    ASP.NET Version:1.1.432 2.2300





    What is happening is that I try to catch the image using the following:

    Private Sub Add30_Preview(B yVal sender As Object, ByVal e As EventArgs)
    Dim imgContent as Object = request.form("I mageUpLoad")
    Dim imgStream as Stream
    imgStream = imgContent.Post edFile.InputStr eam


    It is clear that the error occurs at the last two lines, because if I
    comment them out, everything goes fine (except that the images cannot be
    "handled", and therefore do not come up in the preview).

    TIA.
    ....Geshel
    --
    *************** *************** *************** *************** ***********
    * My reply-to is an automatically monitored spam honeypot. Do not use *
    * it unless you want to be blacklisted by SpamCop. Please reply to my *
    * first name at my last name dot org. *
    *************** *************** *************** *************** ***********
    * "I contend that we are both atheists. I just believe in one fewer *
    * god than you do. When you understand why you dismiss all the other *
    * possible gods, you will understand why I dismiss yours." *
    * - Stephen F. Roberts *
    *************** *************** *************** *************** ***********
    * "Anyone who believes in Intelligent Design ("creationis m") is just *
    * as ignorant, irrational and ill-educated as someone who believes *
    * that the world is a flat disc, that the Sun circles the Earth or *
    * that there really is a tooth fairy. Darwinism has an overwhelming *
    * foundation of evidence that can be tested and reproduced. *
    * *
    * "Intelligen t Design, on the other hand, has no evidence at all; not *
    * one single shred of testable proof. As such, Intelligent Design is *
    * Religious Mythology, and has no right whatsoever to be in our *
    * Science classrooms." - 99.99+% of Scientists *
    *************** *************** *************** *************** ***********
    Mignon McLaughlin once said that "A nymphomaniac is a woman [who is] as
    obsessed with sex as the average man." Unfortunately, since true
    nymphomaniacs are so rare, this means that it takes an extraordinary
    woman to keep up with an ordinary man.
    *************** *************** *************** *************** ***********


    Comment

    • Cerebrus

      #3
      Re: Object variable or With block variable not set

      Hi,
      [color=blue][color=green]
      >> Private Sub Add30_Preview(B yVal sender As Object, ByVal e As EventArgs)[/color][/color]
      1. >> Dim imgContent as Object = request.form("I mageUpLoad")
      2. >> Dim imgStream as Stream
      3. >> imgStream = imgContent.Post edFile.InputStr eam
      [color=blue][color=green]
      >> It is clear that the error occurs at the last two lines, because if I
      >> comment them out, everything goes fine (except that the images cannot be
      >> "handled", and therefore do not come up in the preview)[/color][/color]

      IMHO, It is the first line and not the last 2 lines that have the
      problem.
      The HttpPostedFile. InputStream method *does* return a System.IO.Strea m
      object.

      So, the problem in my view is how you are constructing your
      HttpPostedFile object. Assuming that "request" is an HttpRequest
      object, then it's Form property would return a NameValueCollec tion of
      all the Form items. That is where you need to pay attention.

      Cor wrote :
      [color=blue][color=green]
      >> This is a class where is written "must inherit" so it cannot used as this,
      >> beside that you have to instance a class as new.[/color][/color]

      Err... Cor, now I'm a little confused. I understand that a class that
      is "MustInheri t" cannot be instantiated directly, (using the New
      keyword), but I think that it can still hold an object of that type.

      For instance, is this code wrong ? :-
      ---------------------------------------------------
      Dim MyFile As HttpPostedFile
      Dim MyStream As System.IO.Strea m

      MyFileCollectio n = Request.Files
      MyFile = MyFileCollectio n(0)

      ' Initialize the stream.
      MyStream = MyFile.InputStr eam
      ---------------------------------------------------

      Regards,

      Cerebrus.

      Comment

      • Neo Geshel

        #4
        Re: Object variable or With block variable not set

        Cerebrus wrote:[color=blue]
        > Hi,
        > [color=green][color=darkred]
        >>> Private Sub Add30_Preview(B yVal sender As Object, ByVal e As EventArgs)[/color][/color]
        > 1. >> Dim imgContent as Object = request.form("I mageUpLoad")
        > 2. >> Dim imgStream as Stream
        > 3. >> imgStream = imgContent.Post edFile.InputStr eam
        > [color=green][color=darkred]
        >>> It is clear that the error occurs at the last two lines, because if I
        >>> comment them out, everything goes fine (except that the images cannotbe
        >>> "handled", and therefore do not come up in the preview)[/color][/color]
        >
        > IMHO, It is the first line and not the last 2 lines that have the
        > problem.
        > The HttpPostedFile. InputStream method *does* return a System.IO.Strea m
        > object.
        >
        > So, the problem in my view is how you are constructing your
        > HttpPostedFile object. Assuming that "request" is an HttpRequest
        > object, then it's Form property would return a NameValueCollec tion of
        > all the Form items. That is where you need to pay attention.
        >
        > Cor wrote :
        > [color=green][color=darkred]
        >>> This is a class where is written "must inherit" so it cannot used as this,
        >>> beside that you have to instance a class as new.[/color][/color]
        >
        > Err... Cor, now I'm a little confused. I understand that a class that
        > is "MustInheri t" cannot be instantiated directly, (using the New
        > keyword), but I think that it can still hold an object of that type.
        >
        > For instance, is this code wrong ? :-
        > ---------------------------------------------------
        > Dim MyFile As HttpPostedFile
        > Dim MyStream As System.IO.Strea m
        >
        > MyFileCollectio n = Request.Files
        > MyFile = MyFileCollectio n(0)
        >
        > ' Initialize the stream.
        > MyStream = MyFile.InputStr eam
        > ---------------------------------------------------
        >
        > Regards,
        >
        > Cerebrus.
        > [/color]

        When I try out your code snippet there, I get the following error:

        Compiler Error Message: BC30002: Type 'HttpPostedFile ' is not defined.
        Source Error:
        Line 203: End Sub
        Line 204: Private Sub Add30_Preview(B yVal sender As Object, ByVal e As
        EventArgs)
        Line 205:Dim MyFile As HttpPostedFile
        Line 206:Dim imgStream As System.IO.Strea m
        Line 207:

        So it appears that no, the code is not correct.

        TIA.
        ...Geshel
        --
        *************** *************** *************** *************** ***********
        * My reply-to is an automatically monitored spam honeypot. Do not use *
        * it unless you want to be blacklisted by SpamCop. Please reply to my *
        * first name at my last name dot org. *
        *************** *************** *************** *************** ***********
        * “I contend that we are both atheists. I just believe in one fewer *
        * god than you do. When you understand why you dismiss all the other *
        * possible gods, you will understand why I dismiss yours.” *
        * - Stephen F. Roberts *
        *************** *************** *************** *************** ***********
        * “Anyone who believes in Intelligent Design (“creationism ”) is just *
        * as ignorant, irrational and ill-educated as someone who believes *
        * that the world is a flat disc, that the Sun circles the Earth or *
        * that there really is a tooth fairy. Darwinism has an overwhelming *
        * foundation of evidence that can be tested and reproduced. *
        * *
        * “Intelligent Design, on the other hand, has no evidence at all;not *
        * one single shred of testable proof. As such, Intelligent Design is *
        * Religious Mythology, and has no right whatsoever to be in our *
        * Science classrooms.” - 99.99+% of Scientists *
        *************** *************** *************** *************** ***********
        Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
        obsessed with sex as the average man.” Unfortunately, since true
        nymphomaniacs are so rare, this means that it takes an extraordinary
        woman to keep up with an ordinary man.
        *************** *************** *************** *************** ***********

        Comment

        • Neo Geshel

          #5
          Re: Object variable or With block variable not set

          Cerebrus wrote:[color=blue]
          > For instance, is this code wrong ? :-
          > ---------------------------------------------------
          > Dim MyFile As HttpPostedFile
          > Dim MyStream As System.IO.Strea m
          >
          > MyFileCollectio n = Request.Files
          > MyFile = MyFileCollectio n(0)
          >
          > ' Initialize the stream.
          > MyStream = MyFile.InputStr eam
          > ---------------------------------------------------
          >
          > Regards,
          >
          > Cerebrus.
          > [/color]

          As well, when I do the following:

          Private Sub Add30_Preview(B yVal sender As Object, ByVal e As EventArgs)
          If Nothing <> ImageUpLoad.Pos tedFile Then
          Dim imgStream As Stream = ImageUpLoad.Pos tedFile.InputSt ream
          Dim imgLen As Integer = ImageUpLoad.Pos tedFile.Content Length
          Dim imgType as String =
          ImageUpLoad.Pos tedFile.Content Type.ToString() .ToLower()
          End If

          I get the following error message:

          Compiler Error Message: BC30451: Name 'ImageUpLoad' is not declared.
          Source Error:
          Line 203: End Sub
          Line 204: Private Sub Add30_Preview(B yVal sender As Object, ByVal e As
          EventArgs)
          Line 205: If Nothing <> ImageUpLoad.Pos tedFile Then
          Line 206: Dim imgStream As Stream = ImageUpLoad.Pos tedFile.InputSt ream
          Line 207: Dim imgLen As Integer = ImageUpLoad.Pos tedFile.Content Length


          The problem is, ImageUpLoad is properly declared in the code!! It is
          properly declared as an HtmlInputFile in the Page_Load in the following
          manner:

          Dim imgupload As New HtmlInputFile
          myForm.Controls .Add(imgupload)
          imgupload.ID = "ImageUpLoa d"
          imgupload.Accep t = "image/jpg"


          Please Help!
          TIA.
          ...Geshel
          --
          *************** *************** *************** *************** ***********
          * My reply-to is an automatically monitored spam honeypot. Do not use *
          * it unless you want to be blacklisted by SpamCop. Please reply to my *
          * first name at my last name dot org. *
          *************** *************** *************** *************** ***********
          * “I contend that we are both atheists. I just believe in one fewer *
          * god than you do. When you understand why you dismiss all the other *
          * possible gods, you will understand why I dismiss yours.” *
          * - Stephen F. Roberts *
          *************** *************** *************** *************** ***********
          * “Anyone who believes in Intelligent Design (“creationism ”) is just *
          * as ignorant, irrational and ill-educated as someone who believes *
          * that the world is a flat disc, that the Sun circles the Earth or *
          * that there really is a tooth fairy. Darwinism has an overwhelming *
          * foundation of evidence that can be tested and reproduced. *
          * *
          * “Intelligent Design, on the other hand, has no evidence at all;not *
          * one single shred of testable proof. As such, Intelligent Design is *
          * Religious Mythology, and has no right whatsoever to be in our *
          * Science classrooms.” - 99.99+% of Scientists *
          *************** *************** *************** *************** ***********
          Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
          obsessed with sex as the average man.” Unfortunately, since true
          nymphomaniacs are so rare, this means that it takes an extraordinary
          woman to keep up with an ordinary man.
          *************** *************** *************** *************** ***********

          Comment

          • Cerebrus

            #6
            Re: Object variable or With block variable not set

            Neo wrote :
            [color=blue][color=green]
            >> So it appears that no, the code is not correct.
            >> TIA.
            >> ...Geshel[/color][/color]

            Actually not, Neo, since the code was copied from an MSDN page, so it
            *is* correct. HttpPostedFile is not recognized probably since you need
            to import the System.Web namespace. When I asked Cor Ligthert if the
            code was *wrong*, I didn't mean syntactically wrong, I just meant
            Logically wrong.
            [color=blue][color=green]
            >> It is properly declared as an HtmlInputFile in the Page_Load...[/color][/color]

            There you have a problem ! Declare it outside of any method, so it will
            be a global variable. Then you can instantiate it within the Load
            method. Since you have declared it within the Page_Load method, it goes
            out of context the moment the Page_Load method completes. Therefore,
            when you reference it within another method, the compiler will ring the
            alarm bells, saying the variable is not declared !

            Outside any method :
            -------------------------------
            Dim imgupload As New HtmlInputFile

            Within Page_Load method :
            ----------------------------------------
            imgUpload = New HtmlInputFile ()

            Now go ahead and use it in your Add30_Preview method.

            HTH,

            Regards,

            Cerebrus.

            Comment

            • Cerebrus

              #7
              Re: Object variable or With block variable not set

              BTW, that's one hell of a signature you've got !

              Comment

              Working...