|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
||||
|
||||
|
Common causes of: Object reference not set to an instance of an object error
(mods, you might want to make this a sticky)
Considering the large number of "Object reference not set to an instance of an object" errors abound, I figured its my "public duty" to give the main reasons, and thus... reduce the number of superflous threads created on the matter: Cause one: Not declaring variables! Yes, I know it sounds obvious, but MAKE SURE that you've explicity declared the variable, and don't forget to use the appropriate scope! One common mistayke is when working with Codebehind Classes, consider: XHTML / Tags: Code:
<body> <asp:Literal id="litOne" runat="server" /> </body> ASP.Net Codebehind Class Code:
Public Class PageOne Inherits System.Web.UI.Page Dim strHello As String Sub Page_Load(ByVal e As System.EventArgs) Handles MyBase.Load strHello = "Hello!" litOne.Text = strHello End Sub End Class Remember! Codebehinds arn't handled the same way as inline/<script> pages! You need to declare EVERY control you want to interact with in the codebehind class ...Do this just after the Inherits statement: Code:
Public Class PageOne Inherits System.Web.UI.Page Protected WithEvents litOne As System.Web.UI.WebControls.Literal ... Personally, I use "Protected WithEvents" as its the most flexible and "protects" the object, feel free to use other scopes and attributes as you see fit. Cause 2: Bad scoping! Indeed... One common cause is this: Code:
Sub Page_Load(ByVal e As System.EventArgs) Handles MyBase.Load Private strHello As String End Sub Sub Button1_Click(ByVal e As System.EventArgs) Handles Button1.Click strHello = "Hello!" End Sub This won't work because strHello is accessible only by Sub Page_Load Bad inits/constructs Remember, some classes have constuctors Consider DirectoryInfo: Code:
Sub Page_Load() ....
Dim dirInfo As System.IO.DirectoryInfo
dirInfo.GetDirectory("C:\")
End Sub
In the above example, we've only defined dirInfo *AS* a DirectoryInfo class, we haven't actually created it So we need to do this instead: Code:
Sub Page_Load() ....
Dim dirInfo As System.IO.DirectoryInfo
dirInfo = New System.IO.DirectoryInfo("C:\")
End Sub
That should do it.... Remember, ALWAYS refer to the MSDN Library when enquiring about classes and their initilisation (sp?) methods.... why trust some "1337 script kiddie!" site when you've got the official reference at http://www.msdn.com ? Or better yet, get the compiled version on CD, don't need to worry about long download times or the constant "was this article of any use to you?" screen. HTH -1337_d00d (and yes, I'm aware of the "mistayke" thing, it was supposed to be ironic, but clearly the humour is lost on you) |
|
#2
|
||||
|
||||
|
very nice job, dude... I see you really got into it, eh?
![]() we already have about 5 stickies in this forum, I'll consult with the others moderators and we'll see if there is room for another one... this might as well be attached to existing sticky tutorial. anyways, thanks - this would probably going to be very useful information. |
|
#3
|
||||
|
||||
|
"Just doing my part"
...I figured I needed to get back into the mod's good books after flaming all the newbs for a while last week ![]() |
|
#4
|
||||
|
||||
|
lol
you didn't flame anyone, and even if you did it was justified as they had non standard code! *shudder* ![]() well, as you see this thread has been stuck already, now let's hope ppl will read it before posting problems of that kind. |
|
#5
|
||||
|
||||
|
patience David... patience, climb the ladder...
today... sticky forum topics tomorrow... THE WORLD!! muwahahahahahhhaha!!! |
|
#6
|
|||
|
|||
|
Your a pro
Yo dude
You'll probably want to shoot me, but I have this exact error you so passionately talk about. I have read through your posting but it still hasn't helped me and wonder if you would mind having a look see. I'm quite a novice in .net but have the coding backround just need a slight hand. thanks in advance. |
|
#7
|
||||
|
||||
|
Quote:
post your problem as new thread and we will see if we can help. in your post, give full details aboout what exactly is wrong, along with relevant code causing errors. |
|
#8
|
||||
|
||||
|
I've been having a few different causes of the error recently too, so here's an ammendum:
Not "IDing" user controls! Consider: XHTML Code:
<body> <MyPrefix:MyControl runat="server" /> </body> ASP.Net Code:
Protected MyControl As MyNamespace.MySite.MyControl Sub Page_Load() Handles MyBase.Load MyControl.MyProperty = MyValue End Sub ...won't work You need to declare the ID="" attribute: XHTML Code:
<body> <MyPrefix:MyControl id="MyControl" runat="server" /> </body> HTH Last edited by 1337_d00d : January 31st, 2005 at 05:29 AM. |
|
#9
|
||||
|
||||
|
Quote:
nope, not good idea. this thread is tutorial, it's pointless to make it something else. new problem deserve to be in its own thread. ![]() |
|
#10
|
|||
|
|||
|
Thanks
Quote:
Will post as a new thread, Look forward to the response! Thank again |
|
#11
|
|||
|
|||
|
wow good Job!!!
wonderful Job
![]() |
|
#12
|
|||
|
|||
|
Phoenix,
I am truly gratefull for this sticky. I suffered from a cause 3(bad init) for two days before you and your post rescued me. I should have known better. Thanks for getting me out of that mess! Cheers, Timo |
|
#13
|
|||
|
|||