Question

I have a code that allow user to select files and get the data assign into array.

private void Load_data_Click(object sender, EventArgs e)
     {
         Stream myStream = null;
         OpenFileDialog openFileDialog1 = new OpenFileDialog();

         openFileDialog1.InitialDirectory = "C:\\DataArray";
         openFileDialog1.Filter = "txt files (*.txt)|*.txt";
         openFileDialog1.FilterIndex = 2;
         openFileDialog1.RestoreDirectory = true;

         if (openFileDialog1.ShowDialog() == DialogResult.OK)
         {
             try
             {
                 if ((myStream = openFileDialog1.OpenFile()) != null)
                 {
                     using (myStream)
                     {
                         string filename = openFileDialog1.FileName;
                         var lineCount = 0;
                         using (var reader = File.OpenText(@filename))
                         {
                             while (reader.ReadLine() != null)
                             {
                                 lineCount++;
                             }
                             for(var count = 0; count < lineCount; ++count)
                             {
                                 var data = reader.ReadLine().Split(',');
                                 GlobalDataClass.dDataArray[count, 0] = double.Parse(data[0]);
                                 GlobalDataClass.dDataArray[count, 1] = double.Parse(data[1]);
                             }
                             ShowGraphData(lineCount);
                         }

                     }
                 }
             }
             catch (Exception ex)
             {
                 MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
             }
         }
     }

Once assign, I call the function showgraphdata to plot zedgraph.

public void ShowGraphData(long lTotalData)
    {
        double[] dx = new double[lTotalData];
        double[] dy = new double[lTotalData];

        for (long li = 0; li < lTotalData; li++)
        {
            dx[li] = GlobalDataClass.dDataArray[li, 0];
            dy[li] = GlobalDataClass.dDataArray[li, 1];

        }
        zedGraphControlStickiness.GraphPane.CurveList.Clear();
        GraphPane StickinessPane = zedGraphControlStickiness.GraphPane;

            // PointPairList holds the data for plotting, X and Y arrays 
            PointPairList spl1 = new PointPairList(dx, dy);

            // Add cruves to myPane object
            LineItem ProductionCurve = StickinessPane.AddCurve("Insertion Force", spl1, Color.Blue, SymbolType.None);
            ProductionCurve.Line.Width = 2.0F;

        zedGraphControlStickiness.AxisChange();
        zedGraphControlStickiness.Invalidate();
        zedGraphControlStickiness.Refresh();
        GlobalDataClass.iTotalReadingPoint = lTotalData;

    }

but when i execute this code, i got this error: "object reference not set to an instance of an object"

Can someone advise me wheres my mistake?tQ

Was it helpful?

Solution 2

Problem : you are trying to call ReadLine() method twice. your stream becomes null by end of the while() loop as you are Reading till the end of file.

while (reader.ReadLine() != null) //<-- reading 1st time
{
 lineCount++;
} //stream becomes null here
for(var count = 0; count < lineCount; ++count)
{
 var data = reader.ReadLine().Split(',');  //<-- reading 2nd time
 GlobalDataClass.dDataArray[count, 0] = double.Parse(data[0]);
 GlobalDataClass.dDataArray[count, 1] = double.Parse(data[1]);
}

Solution : You can use File.ReadLines() method to read the all lines in a file.

Try This:

string filename = openFileDialog1.FileName;
var lineCount = 0;
using (var reader in File.ReadLines(@filename))
{    
  var data=reader.Split(',');
  lineCount ++;
  GlobalDataClass.dDataArray[count, 0] = double.Parse(data[0]);
  GlobalDataClass.dDataArray[count, 1] = double.Parse(data[1]);
}
ShowGraphData(lineCount);

OTHER TIPS

The problem is that you are reading until you reach then end:

    while (reader.ReadLine() != null)
    {
        lineCount++;
    }

Then you are reading more, which will always throw a null reference exception (as each call to readline will return null.

    for(var count = 0; count < lineCount; ++count)
    {
        var data = reader.ReadLine().Split(',');
        GlobalDataClass.dDataArray[count, 0] = double.Parse(data[0]);
        GlobalDataClass.dDataArray[count, 1] = double.Parse(data[1]);
    }

Combine your code segments to read once to fix the issue:

    string line;
    while ((line = reader.ReadLine()) != null)
    {
        var data = line.Split(',');

        GlobalDataClass.dDataArray[lineCount, 0] = double.Parse(data[0]);
        GlobalDataClass.dDataArray[lineCount, 1] = double.Parse(data[1]);

        lineCount++; 
    }

While you iterate over (reader.ReadLine() != null) you are changing the position to the EOF. Please try smth like that

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            var count = 0;
            using (var reader = File.OpenText(@openFileDialog1.FileName))
            {
                var str = reader.ReadLine();

                while (str != null)
                {
                    var data = str.Split(',');
                    GlobalDataClass.dDataArray[count, 0] = double.Parse(data[0]);
                    GlobalDataClass.dDataArray[count, 1] = double.Parse(data[1]);
                    str = reader.ReadLine();
                    count++;
                }
            }

            ShowGraphData(count - 1);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top