OOP problem

  • Thread starter Thread starter rapataa
  • Start date Start date
R

rapataa

hi all,

I'm trying to get the following to work:

I have an objectmodel containing users and groups. A user is part of the
users collection and a group contains users.

-------
public class User {///// contains user methods & property functions}
public class Users : System.Collections.CollectionBase {//// contains add,
remove and item functions}
public class Group
{
public Users users = null;
public void fillUsers()
{
User oUser = new User();
oUser.getUserbyID(4);
Users.Add(oUser)
}
}

PROBLEM: the fillUsers function raises an "Object reference not set to an
instance of an object." error. But, if I use the same code (outside the
Group class) for adding a user to the collection Users, it works fine!!

thanks!
 
Did you initialise the "users"-variable. From the code below it is still
null.

Just try adding:
users = new Users();
before you do Users.Add(oUser);
 
yep, that's it

I changed the

public Users users = null;

to

public Users users = new Users();

in the Groups class, and now it works!

thanks Jako!!
 
Happy to help!

rapataa said:
yep, that's it

I changed the

public Users users = null;

to

public Users users = new Users();

in the Groups class, and now it works!

thanks Jako!!
 
Back
Top