Click here to Skip to main content
14,325,352 members
Rate this:
Please Sign up or sign in to vote.
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

Put a break point here:
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.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100