Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Object Reference Not Set to an Instance of an Object

Posted on 2007-03-20
25
Medium Priority
?
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 16
  • 4
  • 3
  • +1
25 Comments
 
LVL 17

Accepted Solution

by:
ZeonFlash earned 1600 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 400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1600 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
 
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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

610 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