C#コンソールは、プログラムでリストにGoogleスプレッドシートのデータを読み取ることができません

ギャレットPierzchajlo:

私は、GoogleスプレッドシートAPIとC#からGoogleスプレッドシートのオフその検索をプレイするゲームで「ワラント」のコンソールベースの検索ツールに取り組んでいます。もともと私は、Pythonでこれを作り、それが完全に働いたが、私はC#に移動して、私は私のpythonファイルを配布する問題の多くを持っていました。

私は私のプログラム内でリストファイルにそれを試してみて、保存するときに、私は次のように取得するAPIは完全に罰金データを呼び出していると私は打ち上げに求めていたすべてのデータのリストを提示することですが、: Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.

私はまた私が行を呼び出していたデータ型を告げるセクション[1]に追加され、それが(一つだけが「`` `」、フォーマットに倍増していた)と言います: System.Collections.Generic.List``1[System.Object]

using Google.Apis.Auth.OAuth2;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace WarrantSearchProgram
{
    class Program
    {
        static readonly string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly };
        static readonly string ApplicationName = "WarrantSearchProgram";
        static readonly string SpreadsheetId = "SpreadsheetId";
        static readonly string sheet = "Imported Data";
        static SheetsService service;

        //List of Warrant Column Variables... Only nameList is being used for now
        public static IList<object> testOBJ;
        public static List<object> wtStatus;
        public static List<object> wtType;
        public static List<object> wtNum;
        public static IList<object> nameList;
        public static List<object> wtCivName;
        public static List<object> wtDOB;
        public static List<object> wtAddress;
        public static List<object> wtJs;
        public static List<object> wtCharges;
        public static List<object> wtEvidence;
        public static List<object> wtReqOfc;
        public static List<object> wtReqOfcNum;
        static void Main(string[] args)
        {
            //Set console color and other general settings
            Console.Title = "DOJ Warrant Search Program UNOFFICIAL";
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.Green;

            // Initialization of creds and google sheets
            GoogleCredential credential;

            using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
            {
                credential = GoogleCredential.FromStream(stream)
                    .CreateScoped(Scopes);
            }

            // Create Google Sheets API service.
            service = new SheetsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            //First initilization of warrant sheet info, creates and manages variables.
            UpdateSheetData();

            while (true)
            {
                // Main repeating text and SEARCH INPUT
                Console.WriteLine("-----------------------------------------------");
                Console.WriteLine("Please type in a full name to search for warrants.");
                Console.WriteLine("Only ACTIVE warrants will be shown.");
                Console.WriteLine("Type in a warrant number to show extra info, including evidence, on just that warrant");
                Console.WriteLine("-----------------------------------------------");
                Console.Write("Search >>> ");
                string searchName = Console.ReadLine();
                searchName = searchName.ToUpper();
                Console.WriteLine();
                Console.Beep();
                Console.Clear();
            }

        }
        static void UpdateSheetData()
        {

        var range = $"{sheet}!A:F";
            SpreadsheetsResource.ValuesResource.GetRequest request =
                    service.Spreadsheets.Values.Get(SpreadsheetId, range);

            var response = request.Execute();
            IList<IList<object>> values = response.Values;

            if (values != null && values.Count > 0)
            {
                foreach (var row in values)
                {
                    // Calls the row (2nd, name) and displays each name in list
                    Console.WriteLine("{0}", row[1]);
                    Console.WriteLine(row.GetType().ToString());

                    // Attempts to build list of names in program ERROR HERE
                    nameList.Add(row[1]);

                }

            }
            else
            {
                Console.WriteLine("No data found.");
            }
        }
    }
}

私はこれとは何の関係もないコードのセクションを削除ので、その読みやすくするために...あなたが言うことができるように、私が試したIList<object>, List<object>, and List<string>、異なる時点で、それは私のために仕事をしませんでした。

ここでの私の目標は、私は、インデックス、上の検索を実行できることをリストにデータの各列をロードすることで、他のリストから表示マッチングデータ。私はプログラムにデータをロードし、それを分離することができたら、これはすべて行うことは非常に難しいことではありません。行98でエラー

A.アブラモフ:

あなたのコメントあたりのように、ライン98であなたがしようとしているAddに値nameList初期化されないことがあり、。これは、あなたがあなたがエラーを取得する理由は、あるObject reference not set to an instance of an object.- nameListあなたはそれの内部関数を呼び出すことはできませんので、開始されませんでした。あなたはすることがありますインスタンスを呼び出すどこでも前に、それをUpdateSheetData、例えば:

nameList = new List<object>();

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=377229&siteId=1