1

I've deployed my site to my server, and everything works fine...EXCEPT for two pages which throw me a nice yellow

"Object reference not set to an instance of an object."

The first line of the stack trace reveals that somewhere along the lines for those two pages, it's still looking for something on my local PC:

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   MyProject.calendar.calendar.calmaincalendar_DayRender(Object sender, DayRenderEventArgs e) in C:\Users\myuser\MyProject\calendar\calendar.aspx.cs:248

The path it spits out there is on my development machine. There is seemingly no reason that this should be happening on the web server, and the even more curious thing is why it is only happening with two pages.

I have re-built and re-published with my configuration setting as Release. This isn't a new project either --- this was just a routine update local --> publish remote, and I didn't do anything out of the ordinary.

Question: how did this happen, and how can I fix it?

CptSupermrkt
  • 6,844
  • 12
  • 56
  • 87
  • 2
    Almost all cases of NullReferenceException are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Aug 22 '12 at 05:48

2 Answers2

3

This is not the server looking for something on your local machine, but rather the stack-trace showing you debug information specific to the source it was compiled from.

Check your source file calendar.aspx.cs at line 248, there's an object there that (when executed on the server) is indeed null.

Stafford Williams
  • 9,696
  • 8
  • 50
  • 101
  • Hmm, that's what I thought at first --- but when I run the project on my local PC, it works just fine, even though they are looking at the exact same data sources. – CptSupermrkt Aug 22 '12 at 05:45
  • It isn't looking for anything on your PC. That's just source line number information, to help you debug. – John Saunders Aug 22 '12 at 05:47
  • 1
    @CptSupermrkt try putting some logging in there to output the values of the objects on that line. Or you could look into remote-debugging... – Stafford Williams Aug 22 '12 at 05:48
  • 1
    @CptSupermrkt Additionally its strongly recommended to add null checking to objects that you are referencing. If you know that you have an object being populated by a datasource, check the object to ensure it has data before you try to reference anything in that object. – Gibron Aug 22 '12 at 05:53
  • @Gibron Thanks. That's the puzzling thing. The line it's throwing at is "if (e.Day.Date == x.EventDate)". x.EventDate is checked strictly, and the line isn't even reached if x.EventDate is null. So it must be e.Day.Date. But the exact same code, looking at the exact same data source, works on my development PC. – CptSupermrkt Aug 22 '12 at 05:57
  • @CptSupermrkt The annoying thing about null checks is if `x == null` then checking if `x.EventDate == null` will throw a null pointer exception. You would need to check to ensure that `x != null` before trying to check `x.EventDate`. A way to prevent this would be to setup EventDate as a `property` in the class that `x` is an instance of. http://msdn.microsoft.com/en-us/library/w86s7x04.aspx. This way, you can add null checking to the `get` and if null a default value can be used vs. null pointer exception being thrown. – Gibron Aug 22 '12 at 06:03
0

That looks like a null reference exception. Make sure you're assigning all your fields and variables where you call that event. You're quite possibly doing what I do a lot with desktop applications and passing in a null value for DayRenderEventArgs