0

It happened before, I changed my dictionary (Dictionary) into ConcurrentDictionary, and it fixed. But when i changed the things, (dictionarys stays same) things gone wrong.

Here is my full Code: http://pasted.co/fd94e190

It says List is null that is from this method.

Models.GridData getCurrentGrid()
    {
        Models.GridData data = new Models.GridData();
        for (int i = 0; i < metroGrid1.Rows.Count; i++)
        {
            foreach (var item in metroGrid1.Rows[i].Cells)
            {
                if (item is DataGridViewCell)
                {
                    Models.CellData cellData = new Models.CellData();
                    DataGridViewCell cell = item as DataGridViewCell;
                    cellData.column = cell.ColumnIndex;
                    cellData.row = cell.RowIndex;
                    if (string.IsNullOrEmpty((string)cell.Value) && string.IsNullOrWhiteSpace((string)cell.Value))
                    {
                        cellData.value = string.Empty;
                    }
                    else {
                        cellData.value = cell.Value;
                    }
//Occurs here right now
                    data.List.TryAdd(cellData.column.ToString() + " " +      cellData.row.ToString(),cellData);
                }
            }
        }
        return data;
    }


void Save()
    {
        Models.GridData data = new Models.GridData();
        data = getCurrentGrid();
        unsavedChanges = false;


        StreamWriter writer = new StreamWriter(path);
        writer.AutoFlush = true;
        writer.Write(JsonConvert.SerializeObject(data.List));
        writer.Close();
        MetroFramework.MetroMessageBox.Show(this, "Başarıyla kaydedildi", "Yoklama Listesi-Talha Açıkgöz", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
    }

EDIT: Models.cs = http://pasted.co/13a7cb1c

Talha Talip Açıkgöz
  • 1,821
  • 4
  • 15
  • 28
  • 3
    Rather than linking to long code elsewhere, please try to reproduce this in a [mcve] - ideally a console app with hard-coded data. In many cases, the process of reducing your real code to a minimal example reveals the problem. – Jon Skeet Sep 15 '16 at 11:07
  • @JonSkeet Thank you, i will try it but i have very limited time right now :\ – Talha Talip Açıkgöz Sep 15 '16 at 11:08
  • In `Save` method I don't see any initialization of `data.List`, you seem to be missing one. – kiziu Sep 15 '16 at 11:08
  • `i will try it but i have very limited time right now` <= So you thought that you would dump it on everyone else to sort out your code? A little inconsiderate don't you think? – Igor Sep 15 '16 at 11:08
  • I edited some wrong information. Could you please try again to fix that? – Talha Talip Açıkgöz Sep 15 '16 at 11:12
  • @Igor My problem is not just NullReferenceException, it should not error NullReferenceException in ConcurrentDictionary – Talha Talip Açıkgöz Sep 15 '16 at 11:13
  • 1
    Your problem **is** a `NullReferenceException`. You should really read the answer on that question, it will help you solve it. And yes, it can throw an NRE on that LoC that you are pointing out and that is not a bug in the framework but a bug in your code, again read the answer on how to track them down and solve them. – Igor Sep 15 '16 at 11:16
  • 1
    @Igor I knew what that is but i couldn't figure out. Thanks! – Talha Talip Açıkgöz Sep 15 '16 at 11:18

1 Answers1

0

I fixed that, thank you all; but especially thanks to @kiziu . I added

data.List = new ConcurrentDictionary...
Talha Talip Açıkgöz
  • 1,821
  • 4
  • 15
  • 28