Solved

Object Reference Not Set to an Instance of an Object

Posted on 2007-03-20
25
294 Views
Last Modified: 2010-04-23
I keep getting System Null Exception

I have the following in my code-behind
Dim _ecUser As ECS.EC.User

    Private ReadOnly Property EbookPolicyAccepted() As DateTime
        Get
            Return _vcUser.EBookPolicyAccepted
        End Get
    End Property

...

            ElseIf (IsDBNull(Me.EbookPolicyAccepted)) Then
                eBookPolicyMessage.Text = "some text here"

and the error I get on the Me.EbookPolicyAccepted is "Object Reference Not Set to an Instance of an Object"

I know that this DateTime fields are all nulls in the Database Table I'm pulling from.  This check is inside a repeater.
0
Comment
Question by:dba123
  • 16
  • 4
  • 3
  • +1
25 Comments
 
LVL 17

Accepted Solution

by:
ZeonFlash earned 400 total points
ID: 18758540
You have "Dim _ecUser As ECS.EC.User", but I don't see anywhere that you instantiate the object "_vcUser".  It looks like that's where the error is stemming from.

0
 
LVL 7

Expert Comment

by:dctuck
ID: 18758551
Perhaps you need to include the check for a DBNull in the Property, before returning it?

i.e.:

Get
   If _vcUser.EBookPolicyAccepted IsNot DBNull Then
        Return _vcUser.EBookPolicyAccepted
  Else
        Return Nothing
  End If
End Get
0
 
LVL 11

Assisted Solution

by:Babycorn-Starfish
Babycorn-Starfish earned 100 total points
ID: 18758607
Hi,

i don't think you can return null (nothing) for a DateTime because it's a value type. You'd need to return a DateTime with some kind of sentinel values to indicate it represents null e.g. some dates WAYYYYY into the future or something.
0
 
LVL 1

Author Comment

by:dba123
ID: 18758612
the first was just a typo when I was putting it in this thread...whoops.  But it's fine as in the name in my own code.
0
 
LVL 1

Author Comment

by:dba123
ID: 18758632
>>Perhaps you need to include the check for a DBNull in the Property, before returning it?

Well, by default, this is NULL in the table until it's populated with a date.  So it should be fine I would think like I had that property right?  If that property picks up Null, then I would just check for Null.  Is this not possible in a get?
0
 
LVL 1

Author Comment

by:dba123
ID: 18758635
so what's teh solution here....not sure because in any case, it's either gonna pick up a NULL or a date from the database table...
0
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 18758641
Hi again.

put a break point on this

Return _vcUser.EBookPolicyAccepted

and see what the value of _vcUser.EBookPolicyAccepted is or see whether me is a null value.
0
 
LVL 17

Assisted Solution

by:ZeonFlash
ZeonFlash earned 400 total points
ID: 18758665
You could change the data type of the variable holding the date to be an Object.  That way you won't have to worry about representing DBNull in a Date type.  In "_vcUser.EBookPolicyAccepted", the data type would have to be changed...but I'm not sure if that's feasible, depending upon the nature/size of your application.
0
 
LVL 1

Author Comment

by:dba123
ID: 18758673
we don't want to change the data type.  We want to know when that user accepted the agreement which is why we're checking for null or a valid datetime
0
 
LVL 1

Author Comment

by:dba123
ID: 18758706
When I put a breakpoint on the line with the keyword return, it acts like it goes to it then sort of skips down to whatever Sub is being called.  I don't see a way to check it but if I look in the locals, I get that same error if I check it there.
0
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 18758741
If you look at the Output window do you see anything like - FirstChanceException blah blah?
0
 
LVL 1

Author Comment

by:dba123
ID: 18758806
In the Output window I only see that symbols are loaded or not loaded messages.  It totally doesn't even stop at my debug point at the return statement and just goes to my next debug point in line which is a Sub routine.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:dba123
ID: 18758814
my locals say

+EbookPolicyAccepted      {"Object reference not set to an instance of an object."}      Date
0
 
LVL 1

Author Comment

by:dba123
ID: 18758824
I just changed it from a DateTime to Date because our Base user class is setting the value to a Date...so just to be consistant and for it to work right.  But still, I'm still getting the same error.
0
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 18758868
Are you ever actually creating an instance of the _vcUser object?

"Dim _vcUser As ECS.EC.User" creates an object of the type ECS.EC.User, but doesn't actually instantiate it.  So somewhere in your code, are you calling something like:

_vcUser = New ECS.EC.User
0
 
LVL 1

Author Comment

by:dba123
ID: 18758899
shi*.  Ok, yea.  I instantiated it now with this:

Dim _vcUser As New ECS.EC.User

But now, I get a little exclmation error on my debug point at that line for the return saying:

"A breakpoint will not currently be hit.  The Source code is different from the original version"
0
 
LVL 1

Author Comment

by:dba123
ID: 18758930
so then I right clicked that breakpoint and chose the following
Location
checked the box for "Allow the source code to be different than orginal version"

Then I was able to get to it.  But it then throws an exeption on that return, the same one we've gotten all along.
0
 
LVL 1

Author Comment

by:dba123
ID: 18758938
so maybe the way I setup the _vcUser.EBookPolicyAccepted in our user class is the problem?  Since I do not have that project in this one, I can't really debug it.
0
 
LVL 1

Author Comment

by:dba123
ID: 18759001
Ok, you can't grab a Null from the database and then it tries to implicitly convert it to a DateTime which is the problem here.

So what should I set that to initially if the Date obtained is Null for that field.  I'm talking that I probably need to put in an if statement to say if Null, set the EBookPolicyAccepted to be a valid Default DateTIme or something  right?
0
 
LVL 1

Author Comment

by:dba123
ID: 18759015
0
 
LVL 7

Expert Comment

by:dctuck
ID: 18759021
Are you sure that when you selected "Allow the source code to be different than orginal version" it did not change your source back to how it was before it was working?
0
 
LVL 1

Author Comment

by:dba123
ID: 18759028
positive, it didn't change it.
0
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 18759041
In that case, as Babycorn-Starfish posted, you could set the value of the field to be either years in the future, or in the past.  Notably, you can use the Date.MinValue or Date.MaxValue to get the extreme valid values...
0
 
LVL 1

Author Comment

by:dba123
ID: 18759082
correct, what I should have done is this in our user class

Private _EBookPolicyAccepted As DateTime = DateTime.MinValue

thanks much.
0
 
LVL 1

Author Comment

by:dba123
ID: 18759195
damn it, I know that's the answer but still I get the same error.

So far now this is what I have

In my User class I have this

Private _EBookPolicyAccepted As DateTime = DateTime.MinValue
...

    Public Property EBookPolicyAccepted() As DateTime
        Get
            Return _EBookPolicyAccepted
        End Get
        Set(ByVal Value As DateTime)
            _EBookPolicyAccepted = Value
        End Set
    End Property

and in the page I've been talking about all along, in it's code behind this:
Dim _vcUser As New ECS.EC.User

    Private ReadOnly Property EbookPolicyAccepted() As DateTime
        Get
            Return _vcUser.EBookPolicyAccepted
        End Get
    End Property
I am still getting an error, same one!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now