Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I am using the following code for reading a text file.
C#
string filename = Path.Combine(path, comboBox1.SelectedItem.ToString() + ".rbl");

StreamReader sr = new StreamReader(filename);
Rules.Rows.Clear();
string line;
while ((line = sr.ReadLine()) != null)
{

        if (sr.ReadLine().Contains("Bool"))
        {
            string[] line1 = Regex.Split(sr.ReadLine().ToString(), ",");
            if (line1[1] == "And")
            {
                RbAnd.Checked = true;
                RbOr.Checked = false;
            }
            else
            {
                RbAnd.Checked = false;
                RbOr.Checked = true;
            }
        }

        if (sr.ReadLine().Contains(",") && !sr.ReadLine().Contains("Bool"))
        {
            string[] line2 = Regex.Split(sr.ReadLine().ToString(), ",");
            string val1 = line2[0];
            string val2 = line2[1];
            string val3 = "";
            if (line2.Length == 3)
            {
                if (line2[2] != "")
                {
                    val3 = line2[2];
                }
            }

            Rules.Rows.Add(val1, val2, val3);
        }

}
sr.Close();


and it throwing NullReferenceException. Anyone can help me to find out the error in my code.
Posted
Comments
Herman<T>.Instance 28-Oct-14 8:41am    
at what line?
Manu Prasad 28-Oct-14 8:42am    
if (sr.ReadLine().Contains(",") && !sr.ReadLine().Contains("Bool"))
Herman<T>.Instance 28-Oct-14 8:44am    
You use a lot of times ReadLine(). That moves a pointer in the file!
Use the line variable to check what you want
Manu Prasad 28-Oct-14 8:43am    
this is my file

Bool ,Or
Added On,Does not equal,test1
Created On,Contains,test2

My Solution:
C#
using (StreamReader sr = new StreamReader(filename))
{
    Rules.Rows.Clear();
    string line;
    while ((line = sr.ReadLine()) != null)
    {

        if (line.Contains("Bool"))
        {
            string[] line1 = Regex.Split(line, ",");
            if (line1[1] == "And")
            {
                RbAnd.Checked = true;
                RbOr.Checked = false;
            }
            else
            {
                RbAnd.Checked = false;
                RbOr.Checked = true;
            }
        }

        if (line.Contains(",") && !line.Contains("Bool"))
        {
            string[] line2 = Regex.Split(line, ",");
            string val1 = line2[0];
            string val2 = line2[1];
            string val3 = string.Empty;
            if (!string.IsNullOrEmpty(line2[2]) && line2.Length.Equals(3))
            {
                val3 = line2[2];
            }
            Rules.Rows.Add(val1, val2, val3);
        }

    }
    sr.Close();
}
 
Share this answer
 
Comments
Manu Prasad 28-Oct-14 8:51am    
Thank you!!!
Herman<T>.Instance 28-Oct-14 8:52am    
URWelcome
BillWoodruff 28-Oct-14 9:25am    
+5
Manu Prasad 28-Oct-14 9:26am    
What?
Herman<T>.Instance 28-Oct-14 10:22am    
URWelcome == You Are welcome
+5 means he likes the solution too
Yes, tThe StreamReader contructor may throw, indeed (see the documentation: "StreamReader Constructor (String)" at MSDN[^]).
As wise developer, you should handle the exception.
 
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