Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here my code :

C#
		StreamReader reader_forces = new StreamReader ("Suj01_PC_DP_M86_forces.txt"); 
		
		using (reader_forces) {

			string line_f = " ";

						for (int m = 0; m < 8; m++) { 
								line_f = reader_forces.ReadLine ();
						}
			
						data_F = new string[80][]; 
			
						float Somme_x = 0;
						float Somme_y = 0;
						float Somme_z = 0;
			
						int counter_F2 = 0;
						while (line_f != null) { 
								counter_F2++;
								line_f = reader_forces.ReadLine ();  

								if (line_f != null && line_f.Length < 3)
									break;
								
								if ((counter_F2 % 10) != 0) { 
Debug.Log (cpt_F); // I have : 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 ... 28 28 28 28 28 28 28 28 28 28 so each value of cpt_f is repeated 10 times
Debug.Log (line_f); // I have the all lines which are displayed, once each

								string [] split_data_F;
								split_data_F = line_f.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries); // Here I have this error message : NullReferenceException: Object reference not set to an instance of an object

					if (split_data_F [3].Equals ("NaN"))
						continue;
					
								Somme_x += float.Parse (line_f.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries) [1]); 
								Somme_y += float.Parse (line_f.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries) [2]);
								Somme_z += float.Parse (line_f.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries) [3]);


								} else {
					
										data_F [cpt_F] = line_f.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries); 

										Somme_x /= 9;
										Somme_y /= 9;
										Somme_z /= 9;
					
										data_F [cpt_F] [1] = Somme_x.ToString ();
										data_F [cpt_F] [2] = Somme_y.ToString ();
										data_F [cpt_F] [3] = Somme_z.ToString ();
					
										Somme_x = 0;
										Somme_y = 0;
										Somme_z = 0;
					
										cpt_F ++;
								}

						}



My problem is indicated in comments.

Mu purpose is to take a group of 10 lines at each time, and to calculate the average of each group.

Thank you in advance !
Posted
Updated 26-May-15 20:37pm
v3

NullReferenceException is thrown when the object that is making the call is null.

Check if the variables (lines_data, line_f) that are calling "Split" are not null.
 
Share this answer
 
Comments
Coralie B 22-May-15 4:34am    
Yes there are some data which = 0
But in the loop I have indicated that the reader runs until the end of the text file : while (line != null)
So how can I indicate I take data until the end of the text file but also I take all data even if = 0 ?
vinayvraman 22-May-15 5:43am    
This is what the MSDN documentation for ReadLine says

"A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r"), or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does not contain the terminating carriage return or line feed. The returned value is null if the end of the input stream is reached."

So according to the above description and looking at your code from line 97 to line 111, it seems to me that the ReadLine is returning null. And hence "line" is null thus "lines_data" is also null.

Changing your code to something like the below code may help you.

while ((line = reader_trajectoires.ReadLine ()) != null) {
counter++;

if ((counter % 9) == 1)
continue;

string lines_data;
lines_data = line;
data [cpt] = lines_data.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries);


cpt ++;
}
Coralie B 22-May-15 5:56am    
Thank you for your help, I've just tried your solution, and I have again the same problem..
You did not tell us where the error occurs, but I am assuming it is at the lines:
C#
split_data_F = line_f.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries);
 
if (split_data_F [3].Equals ("NaN"))
    continue;

You are making the assumption that the Split command will return at least 4 items, but what if it does not, or does not return anything? You need to check that split_data_F is not null, and also how many elements it contains. Never assume that the data will be valid.
 
Share this answer
 
Comments
Coralie B 22-May-15 5:48am    
I understand what you mean and I try to see what you said.
But in fact, I think the problem with line_f, it's that :
while (line_f != null) {
To my mind, the loop doesn't run at all because the first datum is : 0,00 (it's the time)

How can I write that I want to read all the line of my text file, but when there is 0, I want to continue anyway
Richard MacCutchan 22-May-15 5:56am    
0,00 is not null, it is a string of four characters. And that has nothing to do with the error that you reported.
Coralie B 22-May-15 5:58am    
So what is the problem ?

I've ever used this code for another text file with another form. And it runs... But as soon as I use it differently, for another text file, I have problems
Richard MacCutchan 22-May-15 6:24am    
I told you what the problem is, you are trying to use references that have no value. You need to step through the code with the debugger to find the exact point at which the error occurs, and check all the variables to see which one(s) is/are not valid. Like I said above, do not make assumptions in your code, check everything.
Coralie B 22-May-15 8:07am    
int counter = 0;
while (line != null) {
counter++;
line = reader_trajectoires.ReadLine ();

if ((counter % 9) == 1)
continue;

string lines_data;
lines_data = line;

[cpt] = lines_data.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries);

cpt ++;

}

I saw with the debugger that in this part, cpt = 0 (ever)
So data[cpt] = null thoughout
But the string lines_data (and also line) has :
"0.04\t0\t0\t0\t-0.407847\t0.824162\t-0.047091\t-0.518739\t0.872472\t0.023845\t-0.615601\t0.928889\t0.143595\t-0.6…"
So we read the line n°3

Thus, we read line but it seems we don't save , and the other problem is that we don't read good line, because we have to read 1 line / 10 ; here we read line[2], so there is a mistake.

Do you see where comes the problem ?
Your problem is as described by goodpeapul above (which I missed). You have two loops where you read from your file inside the loop, but fail to test for end of data (null returned from read). You need to change the following pieces of code:
C#
while (line != null) { 
    counter++;
    line = reader_trajectoires.ReadLine ();

// change to 

while ((line = reader_trajectoires.ReadLine ()) != null) { 
    counter++;


C#
// check that this is correct
for (int m = 0; m < 7; m++) { 
    line_f = reader_forces.ReadLine (); 
}


C#
while (line_f != null) { 
    counter_F2++;
    line_f = reader_forces.ReadLine (); 

// change to

while ((line_f = reader_forces.ReadLine ()) != null) { 
    counter_F2++;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900