System.NullReferenceException:对象引用未设置为对象的实例
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();
我得到newuserorg[0]的空值。CostCenter
我正在使用Google Admin API和C#来更新用户的组织详细信息。谢谢
UPDATE:现在可以工作了,我只是忘了实例化。谢谢
您的数组有10个空槽。您需要用新对象填充它们。
for (int i = 0; i < newuserorg.Length; i++)
newuserorg[i] = new UserOrganization();
是否尝试为UserOrganization实例赋值?
然后尝试:
UserOrganization newuserorg = new UserOrganization();
newuserorg.setCostCenter = "test";
newuserorg.setDepartment = "test";
...
...
以下是您尝试(可能(要做的事情的完整构造函数:
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; }
}