Object Reference Not Set to an Instance of an Object

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.
LVL 1
dba123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZeonFlashCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dctuckCommented:
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
Babycorn-StarfishCommented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

dba123Author Commented:
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
dba123Author Commented:
>>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
dba123Author Commented:
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
Babycorn-StarfishCommented:
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
ZeonFlashCommented:
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
dba123Author Commented:
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
dba123Author Commented:
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
Babycorn-StarfishCommented:
If you look at the Output window do you see anything like - FirstChanceException blah blah?
0
dba123Author Commented:
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
dba123Author Commented:
my locals say

+EbookPolicyAccepted      {"Object reference not set to an instance of an object."}      Date
0
dba123Author Commented:
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
ZeonFlashCommented:
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
dba123Author Commented:
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
dba123Author Commented:
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
dba123Author Commented:
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
dba123Author Commented:
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
dba123Author Commented:
0
dctuckCommented:
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
dba123Author Commented:
positive, it didn't change it.
0
ZeonFlashCommented:
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
dba123Author Commented:
correct, what I should have done is this in our user class

Private _EBookPolicyAccepted As DateTime = DateTime.MinValue

thanks much.
0
dba123Author Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.