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.
        }

In line 4 you create a variable to hold the table. Where is the line where you actually create the table?

Line 16 is a method definition. Where is the "inventoryTable = <some code that creates it>" line?

It looks like you are missing the inventoryTable = new DataTable(); line.

Edited 6 Years Ago by Momerath: n/a

Right now, I am just following verbatim what is in the book, with the exception of different variable names. Below is everything the book has for this to create the DataTable within the GridView control.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ReneBookForm.Domain;

namespace ReneBookForm
{
    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();                            
        }

        void CreateDataTable()                      
        {
            // Create table.
            DataColumn bookBookIDColumn         = new DataColumn("BookID", typeof(int));
            DataColumn bookTitleColumn          = new DataColumn("Title", typeof(string));
            DataColumn bookAuthorColumn         = new DataColumn("Author", typeof(string));
            DataColumn bookSeriesColumn         = new DataColumn("Series", typeof(string));
            DataColumn bookDescriptionColumn    = new DataColumn("Description", typeof(string));
            DataColumn bookISBNColumn           = new DataColumn("ISBN", typeof(string));
            DataColumn bookStatusColumn         = new DataColumn("Status", typeof(string));
            
            inventoryTable.Columns.AddRange(new DataColumn[] {  bookBookIDColumn, bookTitleColumn, 
                bookAuthorColumn, bookSeriesColumn, bookDescriptionColumn, bookISBNColumn, bookStatusColumn });
            
            foreach (Book c in listBooks)         
            {
                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;  
        }
    }
}

There is also a class Book that just defines the members.

The book is wrong. Change line 17 to

DataTable inventoryTable = new DataTable();

This should fix your problem, but might create some others (without knowing everything they are trying to do, it's hard to tell where to put it. You could place inventoryTable = new DataTable(); between lines 30-31 and also solve the problem).

What book is this, by the way?

Comments
Thanks! Fast and very helpful!

The book is Pro C# 2008 and the .NET 3.5 Platform (pages 800 - 801).

I changed line 17 and that works perfectly.

Thanks for your help! Now I'm just a little worried about the rest of the book and whether it is wrong also :-)

I actually have that book, and looked at the code. It's wrong :) In the code you can download from the publishers webpage, they added the change to line 17.

Thanks again! I am doing some more further past that and I think they have more code wrong in there also. I will check out their site and see what they have there.

This question has already been answered. Start a new discussion instead.