Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I want a program that scans a list(doctors.txt) and determines if the first and second letter (the ones entered into textbox 1&2) matches a name on the list. The names that match should popup in a messagebox. I can get it to work in as a c++ console app, but not in a c# form app.

I'm getting the following error: "Object reference not set to an instance of an object"; the error shows on the following code: "name = temp.ToCharArray();" I googled the error message, but I'm too much of a noob to make anything out of what I found. Thanks in advance for any help.

The below assumes a file named "doctors.txt" exists at "C:\"

C#
namespace doc_forms
{
    public partial class Form1 : Form
    {

        string temp;
        const string path = @"C:\doctors.txt";
        StreamReader FileOut = new StreamReader (path);
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {

            while ((temp = FileOut.ReadLine()) != null)
            {
                temp = FileOut.ReadLine();
                char[] name = new char[10];
                name = temp.ToCharArray();
                string letter = textBox1.ToString();
                string letter2 = textBox2.ToString();
                string nameL1 = name[0].ToString();
                string nameL2 = name[1].ToString();
                if (letter == nameL1 && letter2 == nameL2)
                {
                    MessageBox.Show(temp);
                }
            }
        }
    }
}


*UPDATE* 11/12 8PM
Yep, that solved it, thanks! I know the program is crap. I wasn't able to get farther because I never heard of "Substring". I will polish this turd now. Thanks again.

[Family friendly language please.]
Posted
Updated 13-Nov-10 4:53am
v3

Here it is. First of all remove the second "temp" line after while and you wont see any exceptions.

C#
string temp;
        const string path = @"F:\TEMP\doctors.txt";
        StreamReader FileOut = new StreamReader(path);
        private void button1_Click(object sender, EventArgs e)
        {
            while ((temp = FileOut.ReadLine()) != null)
            {
                if ((textBox1.Text+textBox2.Text)==(temp.Substring(0,2)))
                {
                    MessageBox.Show(temp);
                }
            }
        }

But the whole program seems to be ... not so good
 
Share this answer
 
This design seems to be better, because you can use as many letters as you want to use as a pattern; moreover, you input is in one Textbox.
C#
const string path = @"F:\TEMP\doctors.txt";
       private void button1_Click(object sender, EventArgs e)
       {
           string input = File.ReadAllText(path);
           string pattern = @"^" + textBox1.Text + @"\w*\r";
           foreach (Match m in Regex.Matches(input, pattern, RegexOptions.Multiline))
           {
               MessageBox.Show(m.Value);
           }
       }
 
Share this answer
 
C#
namespace doc_forms{    public partial class Form1 : Form    {        string temp;        const string path = @"C:\doctors.txt";        StreamReader FileOut = new StreamReader (path);        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            while ((temp = FileOut.ReadLine()) != null)            {                temp = FileOut.ReadLine();                char[] name = new char[10];                name = temp.ToCharArray();                string letter = textBox1.ToString();                string letter2 = textBox2.ToString();                string nameL1 = name[0].ToString();                string nameL2 = name[1].ToString();                if (letter == nameL1 && letter2 == nameL2)                {                    MessageBox.Show(temp);                }            }        }    }}
 
Share this answer
 
Comments
fjdiewornncalwe 9-Dec-10 14:39pm    
Hey Raju: This was answered almost a month ago... Please don't add another answer to one that is that old if an approved answer already exists. Cheers.

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