-6

System.NullReferenceException: Object reference not set to an instance of an object

UserOrganization[] newuserorg = new UserOrganization[10];
newuserorg[0].CostCenter = "test";
newuserorg[1].Department = "test";
newuserorg[2].Description = "test";
newuserorg[3].Domain = "demo.com";
newuserorg[4].Symbol = "TWW";
newuserorg[5].Primary = true;
newuserorg[6].Title = "title";
newuserorg[7].Type = "work";
newuserorg[8].Name = "HEY";
newuserorg[9].Location = "PH";
newuserbody.Organizations = newuserorg;
service.Users.Update(newuserbody, email).Execute();

I am getting null value for newuserorg[0].CostCenter

I am using Google Admin API and C# to update the Organization details of users. Thanks.

UPDATE: It works now, I just forgot to instantiate. Thanks.

webbreaker
  • 17
  • 6

3 Answers3

1

Your array has 10 null slots. you need to fill them with new objects.

            for (int i = 0; i < newuserorg.Length; i++)
            newuserorg[i] = new UserOrganization();
Guy
  • 1,232
  • 10
  • 21
  • @gherhald Start by telling what exactly are you trying to do in your code. And use more than on phrase. – john May 29 '15 at 12:21
  • @john This is what I am trying to do: http://stackoverflow.com/questions/30502824/google-admin-directory-api-add-user-employee-details – webbreaker May 29 '15 at 12:22
  • If you class had a constructor like below, it would be easiest: `public class UserOrganization { public UserOrganization() { } public UserOrganization(string name, string address) { Name = name; Address = address; } public string Name { get; set; } public string Address { get; set; } }` – Guy May 29 '15 at 12:23
1

Are you trying to assign values to the UserOrganization instance?

Then try:

UserOrganization newuserorg = new UserOrganization();
newuserorg.setCostCenter = "test";
newuserorg.setDepartment = "test";
...
...
john
  • 613
  • 1
  • 7
  • 25
  • I tried this one before yesterday and it seems it doesn't save on the admin side of Google. I am getting "organizations": { "name": "test", "title": "test", "primary": true, "type": "work", "description": "test" } , instead of "organizations": [ { "name": "test", "title": "test", "primary": true, "type": "work", "description": "test" } ], – webbreaker May 29 '15 at 12:25
  • So your actual problem is that you are trying to send JSON to a service which is rejected because it is not what the API consumes? Maybe you should post a new question. Close this one and ask it properly... – john May 29 '15 at 12:28
  • Yes... That's my problem since yesterday: http://stackoverflow.com/questions/30502824/google-admin-directory-api-add-user-employee-details – webbreaker May 29 '15 at 12:29
1

Here is a full constructor for what you are trying (maybe) to do:

public class Program
{
    public void Main(string[] args)
    {
        UserOrganization[] newuserorg = new UserOrganization[10];
        newuserorg[0] = new UserOrganization("test", "test", "test", "demo.com", "test", true, "test", "test", "test", "test");
    }
}
public class UserOrganization
{
    public UserOrganization()
    {
    }
    public UserOrganization(string costCenter, string department, string description, string domain, string symbol, bool primary, string title ,string type , string name, string location)
    {
        CostCenter = costCenter;
        Department = department;
        Description = description;
        Domain = domain;
        Symbol = symbol;
        Primary = primary;
        Title = title;
        Type = type;
        Name = name;
        Location = location;
    }
    public string Name { get; set; }
    public string CostCenter { get; internal set; }
    public string Department { get; internal set; }
    public string Description { get; internal set; }
    public string Domain { get; internal set; }
    public string Symbol { get; internal set; }
    public bool Primary { get; internal set; }
    public string Title { get; internal set; }
    public string Type { get; internal set; }
    public string Location { get; internal set; }
}
Guy
  • 1,232
  • 10
  • 21
  • Is there a reason why you didn't just edit this in to your other answer? – forsvarir May 30 '15 at 11:30
  • The question was shifting to 2 different directions: having nulls inside the array, and then, as I understood it, how to feed values efficiently into an object. Right now, I'm still not sure what was the issue and the solution, since @gherhald didn't post the working code. I also tried answering [another question asked](http://stackoverflow.com/a/30530035/2381899) – Guy May 30 '15 at 11:40