zedgraph - c# Original error:object reference not set to an instance of an object error -


I have a code that allows the user to choose the file and specify the data in the array.

  Private Zero Load_data_Click (Object Sender, EventArgs e) {stream mystream = null; OpenFileDialog openFileDialog1 = new OpenFileDialog (); OpenFileDialog1.Initial Directory = "C: \\ DataAirere"; OpenFileDialog1.filter = "txt files (* .txt) | * .txt"; OpenFileDialog1.FilterIndex = 2; OpenFileDialog1.RestoreDirectory = True; If (openfileDialog1.ShowDialog () == DialogResult.OK) {try {if ((mystream = openfileDialog1.OpenFile ()) = = null {{mystream} {string filename = openFileDialog1.FileName; var lineCount = 0; (Var reader = Using file.OpenText (@filename) {while (reader.ReadLine ()! = Null) {lineCount ++;} (Var count = 0; count & lt; lineCount; ++ count) for} {Var data = reader.ReadLine (). Split (','); GlobalDataClass.dDataArray [calculation, 0] = double. Text (data [0]); GlobalDataClass.dDataArray [calculation, 1] = double. [1]);} ShowGraphData (Linecount);}}}} Hold (Exception pre) {MessageBox.Show ("Error: File can not be read from disk." Basic error: "+ previous message);}}} < / Code> 

assigned once After doing, I call the Function ShagraphdaData plot plot.

  Public Zero ShowGraphData (long lototdata) {double [] dx = new double [lTotalData]; double [] DI = new Double [lTotalData]; (Long Li = 0; Lee  

But when I execute this code, I get this error: "Object reference is not set to an instance of an object"

Can anyone tell me my mistake? TQ

Problem: You are trying to call The ReadLine () method becomes twice as long as your stream zero in the form of a loop until you are reading up to the end of the file.

  while (reader readline ()! = Null) // & lt; - Reading for the first time {lineCount ++; } // stream (var number = 0, calculation and lieutenant; ++ calculation) becomes futile here {var data = reader.ReadLine (). Split (','); // & lt; - Second time reading GlobalDataClass.dDataArray [calculation, 0] = double. Text (data [0]); GlobalDataClass.dDataArray [Count, 1] = Double. Text (data [1]); }  

Solution: You can use the method to read all the rows in the file.

Try it out:

  string file name = openFileDialog1.FileName; Var LineCount = 0; (Using the File Reedline (@Filename) in Var Reader) {var data = reader.Split (','); LineCount ++; GlobalDataClass.dDataArray [calculation, 0] = double. Text (data [0]); GlobalDataClass.dDataArray [Count, 1] = Double. Text (data [1]); } ShowGraphData (line count);  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -