Object variable or With block variable not set

N

Neo Geshel

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.NullReferenceException: 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:

[NullReferenceException: Object variable or With block variable not set.]
Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o,
Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack) +934
TeamMetz.Add30_Preview(Object sender, EventArgs e) +73
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


Version Information: Microsoft .NET Framework Version:1.1.4322.2300;
ASP.NET Version:1.1.4322.2300





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

Private Sub Add30_Preview(ByVal sender As Object, ByVal e As EventArgs)
Dim imgContent as Object = request.form("ImageUpLoad")
Dim imgStream as Stream
imgStream = imgContent.PostedFile.InputStream


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.
***********************************************************************
 
C

Cor Ligthert [MVP]

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" <[email protected]> schreef in bericht
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.NullReferenceException: 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:

[NullReferenceException: Object variable or With block variable not set.]
Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o,
Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack) +934
TeamMetz.Add30_Preview(Object sender, EventArgs e) +73
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


Version Information: Microsoft .NET Framework Version:1.1.4322.2300;
ASP.NET Version:1.1.4322.2300





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

Private Sub Add30_Preview(ByVal sender As Object, ByVal e As EventArgs)
Dim imgContent as Object = request.form("ImageUpLoad")
Dim imgStream as Stream
imgStream = imgContent.PostedFile.InputStream


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.
***********************************************************************
 
C

Cerebrus

Hi,
1. >> Dim imgContent as Object = request.form("ImageUpLoad")
2. >> Dim imgStream as Stream
3. >> imgStream = imgContent.PostedFile.InputStream

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.Stream
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 NameValueCollection of
all the Form items. That is where you need to pay attention.

Cor wrote :

Err... Cor, now I'm a little confused. I understand that a class that
is "MustInherit" 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.Stream

MyFileCollection = Request.Files
MyFile = MyFileCollection(0)

' Initialize the stream.
MyStream = MyFile.InputStream
 
N

Neo Geshel

Cerebrus said:
Hi,

1. >> Dim imgContent as Object = request.form("ImageUpLoad")
2. >> Dim imgStream as Stream
3. >> imgStream = imgContent.PostedFile.InputStream


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.Stream
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 NameValueCollection of
all the Form items. That is where you need to pay attention.

Cor wrote :


Err... Cor, now I'm a little confused. I understand that a class that
is "MustInherit" 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.Stream

MyFileCollection = Request.Files
MyFile = MyFileCollection(0)

' Initialize the stream.
MyStream = MyFile.InputStream
---------------------------------------------------

Regards,

Cerebrus.

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(ByVal sender As Object, ByVal e As
EventArgs)
Line 205:Dim MyFile As HttpPostedFile
Line 206:Dim imgStream As System.IO.Stream
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.
***********************************************************************
 
N

Neo Geshel

Cerebrus said:
For instance, is this code wrong ? :-
---------------------------------------------------
Dim MyFile As HttpPostedFile
Dim MyStream As System.IO.Stream

MyFileCollection = Request.Files
MyFile = MyFileCollection(0)

' Initialize the stream.
MyStream = MyFile.InputStream
---------------------------------------------------

Regards,

Cerebrus.

As well, when I do the following:

Private Sub Add30_Preview(ByVal sender As Object, ByVal e As EventArgs)
If Nothing <> ImageUpLoad.PostedFile Then
Dim imgStream As Stream = ImageUpLoad.PostedFile.InputStream
Dim imgLen As Integer = ImageUpLoad.PostedFile.ContentLength
Dim imgType as String =
ImageUpLoad.PostedFile.ContentType.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(ByVal sender As Object, ByVal e As
EventArgs)
Line 205: If Nothing <> ImageUpLoad.PostedFile Then
Line 206: Dim imgStream As Stream = ImageUpLoad.PostedFile.InputStream
Line 207: Dim imgLen As Integer = ImageUpLoad.PostedFile.ContentLength


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 = "ImageUpLoad"
imgupload.Accept = "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.
***********************************************************************
 
C

Cerebrus

Neo wrote :

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.

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,821
Messages
2,569,725
Members
45,511
Latest member
Osiris-Team

Latest Threads

Top