Google API:在数组上取得空洞

时间:2015-05-29 12:06:09

标签: 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";
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#来更新用户的组织详细信息。 感谢。

更新:它现在有效,我只是忘了实例化。感谢。

3 个答案:

答案 0 :(得分:1)

您的数组有10个空插槽。你需要用新的物体填充它们。

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

答案 1 :(得分:1)

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

然后尝试:

    for (Command c : commands) {
        c.getStateList().sort(new Comparator<State>() {
                @Override
                public int compare(State o1, State o2) {
                    return o1.getStateDate().compareTo(o2.getStateDate());
                }
        });

        c.getStateList().sort(Collections.reverseOrder());
    }
request.setAttribute("commands", commands);

答案 2 :(得分: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; }
}