Click here to Skip to main content
16,012,110 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Do you know why I have this problem ?

The only difference between these two codes, is :
I take all lines
I take 1 line / 10

The rest of the code is the same in both.


C#
data = new string[1000][];
            
int counter = 0;
while (line != null) {
counter++;
line = reader_trajectoires.ReadLine ();
                
/*if ((counter % 9) != 1) // To take all lines : Error message
continue;*/

string lines_data;
lines_data = line;
data [cpt] = lines_data.Split (new string[] {"\t"}, StringSplitOptions.RemoveEmptyEntries); // "Object reference not set to an instance of an object"

cpt ++;

}


Instead of :

C#
data = new string[1000][];
            
int counter = 0;
while (line != null) {
counter++;
line = reader_trajectoires.ReadLine ();
                
if ((counter % 9) != 1) // To take 1 line / 10 : No Error message
continue;

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

cpt ++;

}


Thank you in advance
Posted
Updated 28-May-15 4:18am
v3
Comments
Andy Lanng 28-May-15 10:42am    
What is the problem?

You are acting in a fundamentally wrong way. You are trying to hard-code some sizes of some arrays. It makes no sense. If the file is bigger then you predict, you will run out of array index, if it is smaller, you wast memory. It's just a big no-no. Arrays can always be allocated dynamically.

First of all, about reading all lines. Isn't that way too simple? Please see: https://msdn.microsoft.com/en-us/library/system.io.file.readalllines%28v=vs.110%29.aspx[^].

See also: https://msdn.microsoft.com/en-us/library/System.IO.File%28v=vs.110%29.aspx[^].

That solves your problem. Now, let's talk about dynamic ways of adding members if the number of them not known in advance. In such cases, always use collection. You can use System.Collections.Generic.List<> (of appropriate element type) and fill it with data. At the end of it, you can always convert it to an array: https://msdn.microsoft.com/en-us/library/x303t819(v=vs.110).aspx[^].

—SA
 
Share this answer
 
How many lines are you talking about?
The chances are that you are exceeding the 1000 "slots" that you have assigned in data when you Split all the lines - when you have the continue code in there you use 1/10 the number of lines. Try using a collection instead of an array:
C#
List<string[]> data = new List<string[]>();

int counter = 0;
do
    {
    counter++;
    line = reader_trajectoires.ReadLine();
    if (line == null) break;

    data.Add(line.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries));
    } while (true);
Note the change to the loop as well.
 
Share this answer
 
Your variable line is null.

Your order of execution is:
1. checking if line is not null
2. then reading line (now it could be null)
3. then using it (it raises null object reference exception if value is null)

Move checking for null a little bit higher and it should be fine:
C#
while ( (line=reader_trajectoires.ReadLine()) != null)
{
    ....
}
 
Share this answer
 
Comments
Coralie B 29-May-15 2:11am    
Thank you, your solution runs !

My variable wasn't null.
I've just replaced :
while (line != null) {
by :
while ( (line=reader_trajectoires.ReadLine()) != null) {
And I removed the firt line after while : "line = reader_trajectoires.ReadLine ();"



Thank you :)

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