Link to home
Start Free TrialLog in
Avatar of dba123
dba123

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of ZeonFlash
ZeonFlash

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dba123
dba123

ASKER

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.
Avatar of dba123

ASKER

>>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?
Avatar of dba123

ASKER

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...
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dba123

ASKER

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
Avatar of dba123

ASKER

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.
If you look at the Output window do you see anything like - FirstChanceException blah blah?
Avatar of dba123

ASKER

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.
Avatar of dba123

ASKER

my locals say

+EbookPolicyAccepted      {"Object reference not set to an instance of an object."}      Date
Avatar of dba123

ASKER

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.
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
Avatar of dba123

ASKER

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"
Avatar of dba123

ASKER

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.
Avatar of dba123

ASKER

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.
Avatar of dba123

ASKER

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?
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?
Avatar of dba123

ASKER

positive, it didn't change it.
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...
Avatar of dba123

ASKER

correct, what I should have done is this in our user class

Private _EBookPolicyAccepted As DateTime = DateTime.MinValue

thanks much.
Avatar of dba123

ASKER

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!