2015-05-29 90 views
-6

System.NullReferenceException:对象不设置到对象的实例谷歌API:让空上阵

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:现在有效,我只是忘了实例化。谢谢。

+0

的可能重复[什么是一个NullReferenceException,我如何修复它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –

+1

提供UserOrganization代码 –

+1

你永远不会实例化一个'UserOrganization',所以你'在'null'上设置属性。 –

回答

1

您的阵列有10个空插槽。你需要填充新的对象。

  for (int i = 0; i < newuserorg.Length; i++) 
      newuserorg[i] = new UserOrganization(); 
+0

@gherhald首先告诉您在代码中究竟做了什么。并使用更多的短语。 – john

+0

@john这是我想要做的:http://stackoverflow.com/questions/30502824/google-admin-directory-api-add-user-employee-details – webbreaker

+0

如果你的类有一个像下面的构造函数,它将是最简单: '公共类UserOrganization { 公共UserOrganization(){ } 公共 UserOrganization(字符串名称,字符串地址) { 名称=名称; Address = address; } public string Name {get;组; } public string Address {get;组; } }' – Guy

1

您是否试图将值分配给UserOrganization实例?

然后尝试:

UserOrganization newuserorg = new UserOrganization(); 
newuserorg.setCostCenter = "test"; 
newuserorg.setDepartment = "test"; 
... 
... 
+0

我在昨天之前试过这个,它似乎没有保存在Google的管理端。 我越来越 “组织”: { “名”: “测试”, “称号”: “测试”, “主”:真实, “类型”: “工作”, “说明” : “测试” } , 代替 “组织”:[ { “姓名”: “测试”, “标题”: “测试”, “主”:真, “类型” :“work”, “description”:“test” } ], – webbreaker

+0

所以你的实际问题是你试图发送JSON到一个被拒绝的服务,因为它不是什么API消耗?也许你应该发布一个新问题。关闭这一个,并正确地问... – john

+0

是的...这是我昨天以来的问题:http://stackoverflow.com/questions/30502824/google-admin-directory-api-add-user-employee-details – webbreaker

1

这里是你想什么(也许)一个完整的构造做:

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; } 
} 
+0

是有一个原因,你不只是编辑这个到你的其他答案? – forsvarir

+0

这个问题正在转向两个不同的方向:在数组内部有空值,然后按照我的理解,如何有效地将值送入对象。现在,我仍然不确定问题和解决方案是什么,因为@gherhald没有发布工作代码。我也试着回答[另一个问题](http://stackoverflow.com/a/30530035/2381899) – Guy