Having a little problem with my code. I'm thinking it is a name somewhere that is the problem, but cannot find it. Getting warning that inventoryTable is never assigned to and will always have default value null.
I have read that this warning can be disabled but when I try to debug my form, it stops on the statement below, saying object reference not set to an instance of an object.
inventoryTable.Columns.AddRange(new DataColumn[] { bookBookIDColumn, bookTitleColumn,
bookAuthorColumn, bookSeriesColumn, bookDescriptionColumn, bookISBNColumn, bookStatusColumn });
public partial class MainForm : Form
{
List<Book> listBooks = new List<Book>();
DataTable inventoryTable;
public MainForm()
{
InitializeComponent();
CenterToScreen();
listBooks.Add(new Book(7, "Test", "Test", "Test", "Test", "Test", "Test"));
CreateDataTable(); // Call method
}
void CreateDataTable() // helper function
{
............
inventoryTable.Columns.AddRange(new DataColumn[] { bookBookIDColumn, bookTitleColumn,
bookAuthorColumn, bookSeriesColumn, bookDescriptionColumn, bookISBNColumn, bookStatusColumn });
foreach (Book c in listBooks) // Iterate over the array list to make rows.
{
DataRow newRow = inventoryTable.NewRow();
newRow["BookID"] = c.BookID;
newRow["Title"] = c.Title;
newRow["Author"] = c.Author;
newRow["Series"] = c.Series;
newRow["Description"] = c.Description;
newRow["ISBN"] = c.ISBN;
newRow["Status"] = c.Status;
inventoryTable.Rows.Add(newRow);
}
bookGridView.DataSource = inventoryTable; // Bind the DataTable to the bookGridView.
}