Click here to Skip to main content
15,886,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi ,

i have a method
public List<VMInfo> GetAllVirtualMachines()
       {
           List<VMInfo> vmInfoList = new List<VMInfo>();
           GetSCVirtualMachine.ParameterSets.AllSet paramSet = new GetSCVirtualMachine.ParameterSets.AllSet();

           try
           {
               //create the command
               GeneratedCommand<VM> vmCommand = GetSCVirtualMachine.Create(paramSet);
               //Using the PowerShellConext to invoke the cmdlet
               //Since PowerShellContext only provides the asynch call, we need to simulate the synch call here

               IAsyncResult ar = this.PSContext.BeginInvoke<VM>(vmCommand, null, null);
               //We are using AutoResetEvent to block the execution until the cmdlet execution is completed
               ar.AsyncWaitHandle.WaitOne();
               //after the callback, we will continue our execution and retrieve the result back
               InvocationResults<VM> results = this.PSContext.EndInvoke<VM>(ar);
               if (results.Errors.Count == 0)
               {
                   foreach (VM vm in results.Output)
                   {
                       //Using our utility class to build our return object
                       VMInfo vmInfo = DataUtil.GetVMInfo(vm);
                       vmInfoList.Add(vmInfo);
                   }
               }
               else
               {
                   StringBuilder errorMsg = new StringBuilder("Failed to get VMs: ");
                   foreach (ErrorInfo error in results.Errors)
                   {
                       errorMsg.AppendLine(error.Problem + " " + error.RecommendedAction);
                   }

                   throw new HostingManagementException(errorMsg.ToString());
               }

           }
           catch (Exception ex)
           {
               throw new HostingManagementException(string.Format(ErrorMessages.VMMManager_GetAllVirtualMachines_Failed_to_retrieve_all_virtual_machines, ex.Message), ex);
           }
           return vmInfoList;
       }




when i call this function it works till VMInfo vmInfo = DataUtil.GetVMInfo(vm);
line in foreach loop for first time when loop move 2nd time its gives error :

Object reference not set to an instance of an object.
Posted
Updated 2-Apr-12 22:33pm
v2

Try to step through and debug your sourcecode. THe error most likely is occurring in GetVMInfo when we run it using the second vm value.
 
Share this answer
 
v2
Put a break point here:
C#
VMInfo vmInfo = DataUtil.GetVMInfo(vm);

and step into the function GetVMInfo, if possible.

If that is not possible stop on the break point, right click on vm and choose "quick watch". you can analyze the object. Note the properties and stop again the second time in the loop. Note the properties again and compare.

It is possible DataUtil is null, but since it exists in the first loop, I doubt the culprit lies here.

Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900