Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. System.NullReferenceException:

System.NullReferenceException:

Scheduled Pinned Locked Moved Database
csharpcssvisual-studiolinqgraphics
4 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    H Martins
    wrote on last edited by
    #1

    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
    
    L 1 Reply Last reply
    0
    • H H Martins

      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
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2
              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.

      Richard DeemingR 1 Reply Last reply
      0
      • L Lost User
                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.

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Unless the Rows collection contains a blank insertion row at the end, in which case i < 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

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        L 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Unless the Rows collection contains a blank insertion row at the end, in which case i < 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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Quite right. But any program worth its salt would be validating the content as it goes through them. And if there is not a blank row at the end then a complete line of data will be missed.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups