GCP c# &#39;Object reference not set to an instance of an object.&#39; when adding object to NetworkInterfaces List<networkinterface>

时间:2019-01-18 18:36:09

标签: google-cloud-platform google-compute-engine google-api-dotnet-client

I'm getting the classic

Object reference not set to an instance of an object

error when adding an Google.Apis.Compute.v1 NetworkInterface object to a list of NetworkInterface's.

Google.Apis.Compute.v1.Data.Instance requestBody = new Google.Apis.Compute.v1.Data.Instance();
requestBody.MachineType = "zones/us-east1-b/machineTypes/n1-standard-1";
requestBody.Name = "lolzorpbn";
requestBody.Scheduling = new Scheduling() { Preemptible = true };
NetworkInterface FailingNetworkObject = new NetworkInterface() { Name = "eth0",  Network = "global/networks/default", AccessConfigs =  new List<AccessConfig>() { new AccessConfig() {  Type = "ONE_TO_ONE_NAT" , Name = "External NAT" } } };
requestBody.NetworkInterfaces.Add(FailingNetworkObject);

The object is instantiated and when inspecting the error it gives no further details.

1 个答案:

答案 0 :(得分:1)

实例化您自己的IList<NetworkInterface>,并将其分配给属性:

Google.Apis.Compute.v1.Data.Instance requestBody = new Google.Apis.Compute.v1.Data.Instance();
requestBody.NetworkInterfaces = new List<NetworkInterface>
{
    new NetworkInterface { ... }
};