2015-05-29 31 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