Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i want to read a text file, line by line. My program reads first line, then (when they like to read second line) they give mi the exception: "index and length must refer to a location within the string".

Code:
C#
try
 {
     using (StreamReader sr = new StreamReader(pot))
     {
         string line= string.Empty;
         while ((line=sr.ReadLine())!=null)
         {

             DASObject dfsa  = new DASObject();

             if (line.Substring(0, 1) == "P")
             {
                 someObject.par= true;
             }
  }}}
 catch (Exception e)
 {
     Console.WriteLine("Error: " + e.Message);
 }


The program return exception in line:
C#
if (line.Substring(0, 1) == "P")


Please help! Thanks :)
Posted
Comments
Thomas Daniels 6-Jan-14 13:10pm    
Is line empty when the exception occurs?
vezo11 6-Jan-14 13:21pm    
Yes, its empty... First time its ok, but second time its empty.

C#
DASObject dfsa  = new DASObject();
 
                        if (line.Substring(0, 1) == "P")
                        {
                            someObject.par= true;
                        }


Should be re-written as:

C#
DASObject dfsa  = new DASObject();
 
                        if (!string.IsEmptyOrNull(line) && line.Substring(0, 1) == "P")
                        {
                            someObject.par= true;
                        }


Which will take care of the line being a null value or if it is empty. Otherwise your first "if" statement is assuming that the line contains something. Your data file has blank lines and blank lines can't be sub-string'ed.
 
Share this answer
 
That means the line has no characters. you have a string of 0 length. You'll have to add a line.Length > 0 && into the if statement or make it a different if to handle a different situation first.
 
Share this answer
 
v2
Check if string is not null and it's length is greater than 0.
If yes, then apply the substring.
 
Share this answer
 
v2
Obviously you hit an empty line. You have to check whether the line has at least one character if you want to read the first character from it.
This will fix it:

C#
if  (!string.IsNullOrEmpty(line)) && (line.Substring(0, 1) == "P")
{
    someObject.par= true;
}
 
Share this answer
 
v2
Comments
Thomas Daniels 6-Jan-14 13:22pm    
+5!
vezo11 6-Jan-14 14:19pm    
Thank you all! :) (y)

Now i have another question.. if i add line, like this
someObject.param= Convert.ToInt64(line.Substring(4, 8));

then program give mi same exception.. How can i resolve this problem, with empty strings?
Adam Zgagacz 6-Jan-14 14:33pm    
1) Before doing conversion you should check if line is at least 13 characters long.
2) Instead of ConvertToInt64 I would rather recommend to use long.TryParse(). With it you can handle cases when substring contains invalid characters.
vezo11 6-Jan-14 14:46pm    
ok thanks :) but i don't know where is the problem, because none of line (in txt file) is not empty..
vezo11 6-Jan-14 15:01pm    
i know what is problem.. the streamreader doesnt read second line, its always null. i dont know where is the problem, if i have code, like this:

using (StreamReader sr = new StreamReader(path))
{
string line= string.Empty;
while ((line= sr.ReadLine()) != null)
{... ... ...}
}
My best guess is that there is a carraige return for that line
 
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