0

I'm having trouble when adding new team to the dataTable. VisualStudio is pointing at line teams.Rows.Add(dr) with NullReference error. Can you please help me?

        private void addTeam(String nazwa)
    {

        DataRow dr = players.NewRow();
        //dr["playerID"] = nazwa;

        dr["nazwa"] = nazwa;
        teams.Rows.Add(dr); //<--there is en error
    }


class Program
{
    static DataTable players ;
    static DataTable teams;
    private DataSet teamMenager;

    static void Main(string[] args)
    {
  • 2
    Where do you initialize the variable "teams"? `teams = new Datatable();` along with all of the column definitions etc. – Steve Py Dec 04 '12 at 22:26
  • You should create the row with the same table you'll add it back to later. – Joel Coehoorn Dec 04 '12 at 22:34
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Dour High Arch Jul 17 '18 at 16:22

1 Answers1

2

The DataTable is not yet initialized

static DataTable teams;

You can initilaize it for example with the default constructor:

static DataTable teams = new DataTable();
static DataTable players = new DataTable();

Although it's not clear why you made them static. This would mean that every instance of Program would share the same DataTable which can be problematic with multiple threads since you need to provide a locking mechanism. Just remove the static and create an instance of Program:

static void Main(string[] args)
{ 
    Program p = new Program();
    p.Start(); // open your form(s) there and add teams or what else
    // ...

Edit: There's something else wrong. You're creating the new DataRow via players.NewRow but adding it to the DataTable teams. That is not allowed. Every DataRow belongs to one DataTable. That cannot be changed and will result in an ArgumentException.

DataRow dr = players.NewRow();
dr["nazwa"] = nazwa;

so add it to players instead:

players.Rows.Add(dr); //<--there is en error
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I'm initializing player DataTable in main. Also it's filled with some data which i can read. Problem is only with inserting function. – user1877249 Dec 04 '12 at 22:57
  • @user1877249: Not sure why you're getting a NullReferenceException then. But you're adding the DataRow to the wrong DataTable. You either have to use teams.NewRow or players.Rows.Add. – Tim Schmelter Dec 04 '12 at 23:02
  • Yep. I corrected that but still getting error. Also changed Main to public but no effect. – user1877249 Dec 04 '12 at 23:14
  • @user1877249: You are using `teams` and `players` DataTables in `addTeam`. But that is not in the class `Program` and you're not referencing the DataTables from `Program`, do you? What DataTables are you referencing? You haven't shown us them, but they are not initialized. – Tim Schmelter Dec 04 '12 at 23:20
  • I think i got it. I redeclarred datatable teams with all columns in addTeam function and it's not crashing. Can you tell me how to make visible columns from main to all other functions? – user1877249 Dec 04 '12 at 23:21
  • @user1877249: Either leave them `public static` and reference them via `Program.teams` or `Program.players` or pass references of `Program` to the methods where you need it. – Tim Schmelter Dec 04 '12 at 23:23