C# googleapi:数组上的Null
System.NullReferenceException:对象引用未设置为对象的实例C# googleapi:数组上的Null,c#,web-services,google-api,google-admin-sdk,C#,Web Services,Google Api,Google Admin Sdk,System.NullReferenceException:对象引用未设置为对象的实例 UserOrganization[] newuserorg = new UserOrganization[10]; newuserorg[0].CostCenter = "test"; newuserorg[1].Department = "test"; newuserorg[2].Description = "test"; newuserorg[3].Domain = "demo.com"; newuser
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]的空值。成本中心
我正在使用谷歌管理API和C#更新用户的组织详细信息。
谢谢
更新:它现在可以工作了,我只是忘了实例化。谢谢。您的阵列有10个空插槽。您需要用新对象填充它们
for (int i = 0; i < newuserorg.Length; i++)
newuserorg[i] = new UserOrganization();
for(int i=0;i
是否正在尝试将值分配给UserOrganization实例
然后尝试:
UserOrganization newuserorg = new UserOrganization();
newuserorg.setCostCenter = "test";
newuserorg.setDepartment = "test";
...
...
以下是您尝试(可能)执行的操作的完整构造函数:
可能重复提供用户组织代码您从未实例化过
UserOrganization
,因此您正在null
上设置属性。为什么要创建UserOrganization
数组?@RajeevKumar我正在尝试更新用户组织的详细信息(部门、成本中心、国家/地区等)使用谷歌管理员API@gherhald首先,告诉你在代码中到底要做什么。john这就是我要做的:如果你的类有一个像下面这样的构造函数,这将是最简单的:public-class-UserOrganization{public-UserOrganization(){}public-UserOrganization(字符串名称,字符串地址){Name=Name;Address=Address;}public string Name{get;set;}public string Address{get;set;}}
我昨天之前试过这个,它似乎没有保存在Google的管理端。我得到的是“组织”:{“Name”:“test”,“title”:“test”,“primary”:true,“type”:“work”,“description”:“test”},而不是“organization”:[{“name”:“test”,“title”:“test”,“primary”:true,“type”:“work”,“description”:“test”}],所以你的实际问题是,你试图将JSON发送到一个被拒绝的服务,因为它不是API所使用的?也许你应该发布一个新的问题。关闭这个问题并正确地问它…是的…这是我昨天以来的问题:你为什么不把它编辑到其他答案中?问题已经转移到两个不同的方向:数组中有空值,然后,据我所知,如何有效地将值输入到对象中。目前,我仍然不确定问题和解决方案是什么,因为@gherhald没有发布工作代码。我还尝试回答了这个问题
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; }
}