System.NullReferenceException:
-
Hi. (I am Portuguese) ********UPDATE******* Looks like I found the problem In the first for() there was a 1 instead of an i. I realized that after increasing zoom level. I apologize. ************** I am a new-by to Visual Studio and C# classes and I am trying to extract a GridView content to a CSV format file. Handling and processing fields seems not too difficult but the extraction is fighting me. I run the compiler, the form shows up, I fill the grid clicking the fill button, it fills OK but when I press the button to export, I get: System.NullReferenceException: 'Object reference not set to an instance of an object.' System.Windows.Forms.DataGridViewCell.Value.get returned null. In both similar lines: writer.Write(dataGridViewMotor1.Rows[i].Cells[j].Value.ToString());
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;namespace Stepping
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void dataGridView1\_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void btnSave01\_MouseClick(object sender, MouseEventArgs e) { TextWriter writer = new StreamWriter(@"H:\\proj\\\_Programacao\\\_Arduino\\ArduinoHM\\Stepper\_Polling\_us\\WinApp\\M01.txt"); for (int i = 0; 1 < dataGridViewMotor1.Rows.Count - 1; i++) { for (int j = 0; j < dataGridViewMotor1.Columns.Count; j++) { if (j == 0) { writer.Write(dataGridViewMotor1.Rows\[i\].Cells\[j\].Value.ToString()); } else { writer.Write("," + dataGridViewMotor1.Rows\[i\].Cells\[j\].Value.ToString()); } } } writer.Close(); MessageBox.Show("Data Exported"); } private void btnFillTestData\_MouseClick(object sender, MouseEventArgs e) { // Line, Abs, Rel, X, C\_t, C\_i, C\_n, MinSpeed, MaxSpeed, Accel, Decel dataGridViewMotor1.Rows.Add(1,0, 120,0,0,0,0, 10, 100, 0.9, 0.9 ); dataGridViewMotor1.Rows.Add(2,0,12
-
Hi. (I am Portuguese) ********UPDATE******* Looks like I found the problem In the first for() there was a 1 instead of an i. I realized that after increasing zoom level. I apologize. ************** I am a new-by to Visual Studio and C# classes and I am trying to extract a GridView content to a CSV format file. Handling and processing fields seems not too difficult but the extraction is fighting me. I run the compiler, the form shows up, I fill the grid clicking the fill button, it fills OK but when I press the button to export, I get: System.NullReferenceException: 'Object reference not set to an instance of an object.' System.Windows.Forms.DataGridViewCell.Value.get returned null. In both similar lines: writer.Write(dataGridViewMotor1.Rows[i].Cells[j].Value.ToString());
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;namespace Stepping
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void dataGridView1\_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void btnSave01\_MouseClick(object sender, MouseEventArgs e) { TextWriter writer = new StreamWriter(@"H:\\proj\\\_Programacao\\\_Arduino\\ArduinoHM\\Stepper\_Polling\_us\\WinApp\\M01.txt"); for (int i = 0; 1 < dataGridViewMotor1.Rows.Count - 1; i++) { for (int j = 0; j < dataGridViewMotor1.Columns.Count; j++) { if (j == 0) { writer.Write(dataGridViewMotor1.Rows\[i\].Cells\[j\].Value.ToString()); } else { writer.Write("," + dataGridViewMotor1.Rows\[i\].Cells\[j\].Value.ToString()); } } } writer.Close(); MessageBox.Show("Data Exported"); } private void btnFillTestData\_MouseClick(object sender, MouseEventArgs e) { // Line, Abs, Rel, X, C\_t, C\_i, C\_n, MinSpeed, MaxSpeed, Accel, Decel dataGridViewMotor1.Rows.Add(1,0, 120,0,0,0,0, 10, 100, 0.9, 0.9 ); dataGridViewMotor1.Rows.Add(2,0,12
for (int i = 0; 1 < dataGridViewMotor1.Rows.Count - 1; i++)
Your compare expression is still wrong. The expression
1 < dataGridViewMotor1.Rows.Count - 1
will always be true. It should be**i** < dataGridViewMotor1.Rows.Count
. And a suggestion: don't rely on videos to teach you how to program. Get a good online tutorial, or book, and learn the actual language first. Using Windows forms and complex controls without understanding the structure of the language properly is a waste of your time. -
for (int i = 0; 1 < dataGridViewMotor1.Rows.Count - 1; i++)
Your compare expression is still wrong. The expression
1 < dataGridViewMotor1.Rows.Count - 1
will always be true. It should be**i** < dataGridViewMotor1.Rows.Count
. And a suggestion: don't rely on videos to teach you how to program. Get a good online tutorial, or book, and learn the actual language first. Using Windows forms and complex controls without understanding the structure of the language properly is a waste of your time.Unless the
Rows
collection contains a blank insertion row at the end, in which casei < dgv.Rows.Count - 1
would be correct. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Unless the
Rows
collection contains a blank insertion row at the end, in which casei < dgv.Rows.Count - 1
would be correct. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer