0

I am gotting a error and i don't know how to resolve:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Here is the code :

private void PictureBox1_Click(object sender, EventArgs e)
    {
        string DataGridResources = "";

        foreach (DataGridViewRow row in SettingsDGV.Rows)
        {
            DataGridResources = row.Cells[0].Value.ToString();
            DataGridResources += '«';
            DataGridResources += row.Cells[1].Value.ToString();
            DataGridResources += '«';
            DataGridResources += row.Cells[2].Value.ToString();
            DataGridResources += '«';
        }

        MessageBox.Show(DataGridResources);
        Application.Exit();
    }

Image of te error

Augusto
  • 1
  • 2
  • 2
    That's *very* strange-looking C code! Please clarify which language it is and/or correct your tags. At a guess, I would think it's C#. – Adrian Mole Feb 28 '21 at 19:21
  • Your code is incomplete and we do not know at which location the exception occurs. Use the debugger to check where the error occurs and to see variables and the call stack. – Bodo Feb 28 '21 at 19:22
  • the error is on the last parentheses ' } '. – Augusto Feb 28 '21 at 19:30
  • Maybe one or more of the values is null. Better check on that before using them.. – TaW Feb 28 '21 at 19:34
  • Thanks @Taw... but i don't know why this error occor. But I got it fixed. I used a code : Convert.ToString(row.Cells[0].Value); And the error doesn't appear anymore. – Augusto Feb 28 '21 at 20:39
  • [Convert](https://learn.microsoft.com/en-us/dotnet/api/system.convert.tostring?view=net-5.0#System_Convert_ToString_System_Object_System_IFormatProvider_) returns: _..the string representation of value, or Empty if value is an object whose value is null. If value is null, the method returns null._ – TaW Feb 28 '21 at 21:05

0 Answers0