Object reference not set to an instance of an object. CAUSED BY.....

B

bwalke

I am saving pictures and files to a directory using VB.NET web pages.
On my development machine everything works fine. Development machine
is running XP Pro, VS.NET, .Net 1.1. When I post the pages on my web
server I get the following errors.

Object reference not set to an instance of an object.
Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Server is running 2000 server, Vs.Net not installed, and .Net 1.0.

What I have come to the conclusion is this code is throwing the error:

strFileName = File1.PostedFile.FileName
strFileName = Path.GetFileName(strFileName)

I have System.IO declared. Any ideas as to why will work on my
development machine and not from a client running off the server.

Thanks you much,

Brett
 
J

Jon Skeet [C# MVP]

I am saving pictures and files to a directory using VB.NET web pages.
On my development machine everything works fine. Development machine
is running XP Pro, VS.NET, .Net 1.1. When I post the pages on my web
server I get the following errors.

Object reference not set to an instance of an object.
Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Server is running 2000 server, Vs.Net not installed, and .Net 1.0.

What I have come to the conclusion is this code is throwing the error:

strFileName = File1.PostedFile.FileName
strFileName = Path.GetFileName(strFileName)

I have System.IO declared. Any ideas as to why will work on my
development machine and not from a client running off the server.

Well, either File1 is null (Nothing), or File1.PostedFile is, or
possibly File1.PostedFile.FileName is. I suggest you add some logging
to find out which it is, and then you'll have a better idea of what you
need to correct.
 
B

bwalke

Thanks for the response. If the File Input Form is left blank then
this section 'File1.PostedFile.FileName' and
'Path.GetFileName(strFileName)' is not accessed. So, if the code is
accessed there should be a value. This code has no problems running on
my development machine but errors when on the server as described in
orginial post. Could this error because by using different versions of
..NET Framework. When testing and trying to debug, after commenting
various sections of my source code, the sections which seem to throw
the object reference error is anything that uses the System.IO
commands. This is declared in namespace, would there be a missing
assembly on the server?

Thank you,

Brett
 
J

Jon Skeet [C# MVP]

Thanks for the response. If the File Input Form is left blank then
this section 'File1.PostedFile.FileName' and
'Path.GetFileName(strFileName)' is not accessed. So, if the code is
accessed there should be a value. This code has no problems running on
my development machine but errors when on the server as described in
orginial post. Could this error because by using different versions of
.NET Framework. When testing and trying to debug, after commenting
various sections of my source code, the sections which seem to throw
the object reference error is anything that uses the System.IO
commands. This is declared in namespace, would there be a missing
assembly on the server?

No, it's not likely to be a missing assembly. What does the stack trace
look like, in full?
 
B

bwalke

To me it doest appear to give much direction.

Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
LumberSpecialties.AddUpdateEmpPic1.cmdAdd_Click(Object sender,
EventArgs e) +254
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() +1263

Thanks
 
J

Jon Skeet [C# MVP]

To me it doest appear to give much direction.

Well, it suggests that it's nothing you're calling - it's a direct
dereferencing of null.

I go back to my previous suggestion - use logging to check each of the
possible causes individually.
 
V

Vlad Gerchikov

Hi,
What I would do, and I guess this is what Jon meant, is something similar to
the folowing:
1. if (File1 == null) MessageBox.Show ("File1 is null");
2. if (File1.PostedFile == null) MessageBox.Show("File1.PostedFile is
null");
3. if (File1.PostedFile.FileName == null)
MessageBox.Show("File1.PostedFile.FileName is null");

See if that helps
Vlad
 
J

Jon Skeet [C# MVP]

I am not familiar with logging. Any tips?

If you search for "logging in ASP.NET" on Google. you should find
plenty of articles. Having not done much ASP.NET myself, I'm afraid I
don't know the ins and outs of it.
 
B

bwalke

I placed this code in my application...

If File1.PostedFile.FileName Is String.Empty Then
Response.Write("File1.PostedFile.FileName is Null")
End If

It still returned with the error: "Exception Details:
System.NullReferenceException: Object reference not set to an instance
of an object."

For some reason unknown to me, else I wouldnt be asking these
questions, the server this page is hosted on, it doesnt appear to
"like" File1.PostedFile and any of it members or instances.

Any ideas from this?
 
J

Jon Skeet [C# MVP]

I placed this code in my application...

If File1.PostedFile.FileName Is String.Empty Then
Response.Write("File1.PostedFile.FileName is Null")
End If

It still returned with the error: "Exception Details:
System.NullReferenceException: Object reference not set to an instance
of an object."

Which it certainly would, if either File1 or File1.PostedFile was
Nothing.
For some reason unknown to me, else I wouldnt be asking these
questions, the server this page is hosted on, it doesnt appear to
"like" File1.PostedFile and any of it members or instances.

Any ideas from this?

Yes - find out whether it's File1 which is Nothing, or
File1.PostedFile.

Note that Nothing is *very* different from String.Empty. One is a
reference to no object at all, the other is a reference to an empty
string.
 
B

bwalke

If I use...(see below) no action is taken, Response.Write does not
fire, whether I select a file or not.

If File1 Is Nothing Then
Response.Write("File1 is Null")
End If

Would this cause object reference error?

Thanks for all your help!!
 
J

Jon Skeet [C# MVP]

If I use...(see below) no action is taken, Response.Write does not
fire, whether I select a file or not.

If File1 Is Nothing Then
Response.Write("File1 is Null")
End If

Would this cause object reference error?

Okay, so it looks like File1 isn't null. Now try File1.PostedFile. Then
(in another test run) try File1.PostedFile.FileName again, this time
checking for null rather an empty string.

Have you tried debugging it and inspecting the variables, by the way?
 
B

bwalke

Ok here is the run down (I think)...

This Statement:
If Not File1 Is Nothing Then
Response.Write("File1 is Null")
End If

File Selected or No File Selected - Response.Write executes

This Statement:
If Not File1.PostedFile Is Nothing Then
Response.Write("File1.PostedFile is Null")
End If

File Selected or No File Selected - Respose.Write does NOT execute.

Appears that the page on the server is not grabing the posted file,
would you concur.

Any causes for this?
 
J

Jon Skeet [C# MVP]

Ok here is the run down (I think)...

This Statement:
If Not File1 Is Nothing Then
Response.Write("File1 is Null")
End If

File Selected or No File Selected - Response.Write executes

This Statement:
If Not File1.PostedFile Is Nothing Then
Response.Write("File1.PostedFile is Null")
End If

File Selected or No File Selected - Respose.Write does NOT execute.

Well, your logic isn't doing what your response says it is - the fact
that it's not writing anything means that File1.PostedFile *is* null.
Appears that the page on the server is not grabing the posted file,
would you concur.

I'd say that File1.PostedFile is null, but without knowing more about
the system I'd hesitate to go further than that.
Any causes for this?

I suggest you ask on the ASP.NET group. As I said before, I haven't
done much ASP.NET.
 
B

bwalke

K, Thanks Jon.

As for the logic and the response part I made change to the IF
statement and not the response.write. Just more or else to see if
anything displayed. I will take this knowledge and pass it on else
where to try and solve my problem.

Brett
 

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

Top