Solved

Object Reference Not Set to an Instance of an Object

Posted on 2007-03-20
25
292 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
If you look at the Output window do you see anything like - FirstChanceException blah blah?
0
 
LVL 1

Author Comment

by:dba123
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:dba123
Comment Utility
my locals say

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

Author Comment

by:dba123
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 7

Expert Comment

by:dctuck
Comment Utility
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
Comment Utility
positive, it didn't change it.
0
 
LVL 17

Expert Comment

by:ZeonFlash
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

10 Experts available now in Live!

Get 1:1 Help Now