Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problems checking for null object

0 views
Skip to first unread message

pbd22

unread,
Mar 13, 2009, 1:35:19 PM3/13/09
to
Hi.

I have a user object and I want to check for nullity.
I want to know if no data has been entered for this
particular object (ie. the user just hit submit w/o
registering... assuming no client validation).

I use:

if (user == null)

which I think is correct. but, i get false. I think the
reason is that the default value for an Int is 0 and
therefor the object is not completely null?

user.first = null
user.last = null
user.day = 0
user.moth = 0
user.year = 0
user.city = null
etc...

thoughts?

thanks.

Peter Duniho

unread,
Mar 13, 2009, 2:01:50 PM3/13/09
to
On Fri, 13 Mar 2009 10:35:19 -0700, pbd22 <dus...@gmail.com> wrote:

> Hi.
>
> I have a user object and I want to check for nullity.
> I want to know if no data has been entered for this
> particular object (ie. the user just hit submit w/o
> registering... assuming no client validation).
>
> I use:
>
> if (user == null)
>
> which I think is correct. but, i get false. I think the
> reason is that the default value for an Int is 0 and

> therefor the object is not completely null? [...]

Without a concise-but-complete code sample demonstrating your problem,
there's no way to know for sure.

But, if you can examine _any_ field of the object, even if that field is
set to "null", then the object reference itself is _not_ null. The "null"
characteristic is of the variable referencing the object, not of the
object itself. By definition, if you have an object to look at, the
variable is not "null".

If you have an object reference that's non-null, and you want to know
whether "no data has been entered for this particular object", then the
object itself needs to have some way of identifying that no data has been
entered. How best to do that depends on exactly how you create the object
based on the user input, and what the object supports in the way of
recording that user input. But, from your example it seems that you might
just look at the object and see if fields you expected to be filled in are
in fact not filled in.

Pete

Tom Overton

unread,
Mar 13, 2009, 3:08:48 PM3/13/09
to

What type of object is "user"?

If it's a regular instance class then your code should work, assuming
if in your code if the user presses submit without doing data entry
then an instance of the class is never created. If no instance of
your object has been created it will have a null reference (nothing on
the heap yet). This won't work with a static class because by the
time you access the object in code to check it, it won't have a null
value anymore.

So if it's a struct or static class you'll just have to inspect each
field to see if values have been assigned.

proxyuser

unread,
Mar 13, 2009, 4:07:43 PM3/13/09
to

"pbd22" <dus...@gmail.com> wrote in message
news:afde0635-2931-4281...@v39g2000yqm.googlegroups.com...

> Hi.
>
> I have a user object and I want to check for nullity.
> I want to know if no data has been entered for this
> particular object (ie. the user just hit submit w/o
> registering... assuming no client validation).
>
> I use:
>
> if (user == null)
>
> which I think is correct. but, i get false. I think the
> reason is that the default value for an Int is 0 and
> therefor the object is not completely null?
>
> user.first = null

Of course - if you can access "first", then there must be something there to
access it with. In other words, user is definitely not null in that case
(unless you get a runtime exception.) user itself has to actually be null,
in which case you cannot write things such as "user.first".


proxyuser

unread,
Mar 13, 2009, 4:19:20 PM3/13/09
to

"pbd22" <dus...@gmail.com> wrote in message
news:afde0635-2931-4281...@v39g2000yqm.googlegroups.com...
> Hi.
>
> I have a user object and I want to check for nullity.
> I want to know if no data has been entered for this
> particular object (ie. the user just hit submit w/o
> registering... assuming no client validation).

Once you figure out your null issues, than one obvious solution is to simply
add a flag to the class that is true whenever data is updated in an object
after the constructor runs. Or better yet, the flag only gets set when a
public accessor sets the data. Something to the effect of:

class A

{

int i;

int j;

bool userUpdated = false;

public int I

{

get

{

return i;

}

set

{

i = value;

userUpdated = true;

}

}

public int J

{

get

{

return j;

}

set

{

j = value;

userUpdated = true;

}

}

private void SomeInternalMethod( )

{

i = i + 1;

j = j + 1;

// no user updates here

}

}


Dawid Rutyna

unread,
Mar 13, 2009, 4:36:30 PM3/13/09
to
(...)

> I use:
>
> if (user == null)
>
> which I think is correct. but, i get false.
(...)

Solutions for you:
(1) override Equals and operator ==, != ..., this is bad idea
(2) write a method like IsEmpty(), better

class Program
{
static void Main(string[] args)
{
User u;

u = new User();
Console.WriteLine(u == null);
u = new User(null, null, 0);
Console.WriteLine(u == null);
u = new User(string.Empty, string.Empty, 0);
Console.WriteLine(u == null);
u = new User("Dawid", "Rutyna", 24);
Console.WriteLine(u == null);


Console.WriteLine(u.IsEmpty());
}
}

class User
{
private string _name;
private string _surname;
private int _age;

public User()
{
_name = string.Empty;
_surname = string.Empty;
_age = 0;
}

public User(string name, string surname, int age)
{
_name = name;
_surname = surname;
_age = age;
}

public bool IsEmpty()
{
return (string.IsNullOrEmpty(this._name) &&
string.IsNullOrEmpty(this._surname) && _age == 0);
}

public override bool Equals(object obj)
{
if (obj == null)
return (string.IsNullOrEmpty(this._name) &&
string.IsNullOrEmpty(this._surname) && _age == 0);
if (this.GetType() != obj.GetType()) return false;

User u = (User)obj;
if (!Object.Equals(_name, u._name)) return false;
if (!Object.Equals(_surname, u._surname)) return false;
if (!_age.Equals(u._age)) return false;

return true;
}

public static bool operator ==(User u1, User u2)
{
return u1.Equals(u2);
}

public static bool operator !=(User u1, User u2)
{
return !u1.Equals(u2);
}
}

Dawid Rutyna


0 new messages