对象引用未设置为Office 365 API实例

时间:2014-08-22 03:31:38

标签: asp.net-mvc office365-apps

我正在尝试将OneDrive中的文件列表拉到列表中,并将其显示到带有MVC应用程序的视图中。这是我的第一次去,所以如果我遗漏了一些明显的东西,我会道歉。我得到的对象引用未设置为对象错误的实例。这是我的代码

public async Task<ActionResult> GetOneDriveFiles()
    {


        List<OneDriveFiles> myFilesList = new List<OneDriveFiles>();


        try
        {
       var fileResults = await _spFilesClient.Files.Take(10).ExecuteAsync();
        var files = fileResults.CurrentPage.OfType<Microsoft.Office365.SharePoint.File>().OrderBy(e => e.Name);


        foreach (File file in files)
        {
            OneDriveFiles myFile = new OneDriveFiles();
            myFile.Id = file.Id;
            myFile.Name = file.Name;
            myFile.Url = file.Url;
            myFile.Size = file.Size;
            myFile.TimeLastModified = file.TimeLastModified;

            myFilesList.Add(myFile);


        }

        }



       catch (RedirectRequiredException redir2)

        {
            return Redirect(redir2.RedirectUri.ToString());


        }
        return View(myFilesList);
    }

这是错误代码:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   O365APIURSAITO.Controllers.<GetOneDriveFiles>d__14.MoveNext() +110
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +84
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +17
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +225
   System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +28
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651188
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

1 个答案:

答案 0 :(得分:0)

我的问题是我忘了确保客户端已创建。要在try语句的左括号之后执行此操作:

 public async Task<ActionResult> GetOneDriveFiles()
    {
        List<OneDriveModel> oneDriveFiles = new List<OneDriveModel>();

        try
        {


            await MyFilesApiSample.EnsureClientCreated();
            var allMyFiles = await MyFilesApiSample.GetMyFiles();
            foreach (var item in allMyFiles)
            {
                OneDriveModel newFile = new OneDriveModel();
                newFile.Name = item.Name;
                newFile.Url = item.Url;
                newFile.Size = item.Size;
                newFile.LastModified = item.TimeLastModified;

                oneDriveFiles.Add(newFile);
            }
        }
        catch (RedirectRequiredException ex)
        {
            return Redirect(ex.RedirectUri.ToString());
        }
        return View(oneDriveFiles);
    }