I am working on a console based search tool for "Warrants" in a game I play that searches off of a google sheet from the google sheets API and C#. Originally I made this on python and it worked perfectly but I had a lot of issues distributing my python file so I moved to C#.
The API is calling the data perfectly fine and I am able to present a list of all the data I was seeking on launch, but when I try and save it to list files inside my program I get the following:
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
I also added in a section that tells me the data type I am calling with row[1] and it says (only one "```", had to double to format):
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.");
}
}
}
}
I removed sections of the code that have nothing to do with this so its easier to read...
As you can tell, I tried IList<object>, List<object>, and List<string>
at different times and it didn't work for me.
My goal here is to load each column of data into a list that I can then perform searches on, index, and display matching data from other lists. This is all not very difficult to do once I can load the data up into the program and separate it. error at row 98