Click here to Skip to main content
15,917,926 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi friends,
As part of a software I am working on, an empty text file("filex.txt") is put into the same folder as the executable by setup installation. I write the activation status(which is just a string) to this file on registration of the software and at every start-up, I read the content of this file to verify registration. The problem is that I get an error when I run that bit of the software so I isolated that bit into a test project and still get an error. The Exception message that I caught says: "Object Reference not set to an instance of an object". The bit of code I tried in the test project is shown below:
C#
//create a streamreader for the filex.txt file
                StreamReader reader = new StreamReader(pathfu);
                try
                {
                    string rline = reader.ReadLine();
                    
                    MessageBox.Show(rline.Trim());
                    MessageBox.Show("This was successful, Thank you");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("The error is: " + ex.Message);
                }
                finally
                {
                    reader.Close();
                }
Posted

Try this:

C#
if (reader != null)
{
    string rline = reader.ReadLine();
    if (!string.IsNullOrEmpty(rline))
    {
        MessageBox.....
    }
}
 
Share this answer
 
v3
Comments
Manas Bhardwaj 1-Aug-11 12:03pm    
this would only prevent the error to be thrown. The actual cause still remains the same. Bad file path????
anycase, my 5! :)
smilerP 2-Aug-11 6:06am    
Thanks
Try:
C#
string rline = reader.ReadLine();
if (rline != null)
   {
   MessageBox.Show(rline.Trim());
   MessageBox.Show("This was successful, Thank you");
   }

If you look at StreamReader.ReadLine[^] it returns null if the end of the file is encountered.
 
Share this answer
 
Comments
smilerP 2-Aug-11 6:06am    
Thank you.
ReadLine() method of StreamReader class works like, if the charLen and charPos (private members of the StreamReader class) is equal to 0 and ReadBuffer() method 0 as well, then ReadLine() method return null.
As a result You are getting null to rline.

So it is clear that we cannt call a method( in this case Trim() ) from a null object.

It might be helpful,

C#
class Program
    {

        static void Main(string[] args)
        {
            ReadFromFile("C:\\Temp\\Test2.txt");
        }

        public static void ReadFromFile(string fileToRead)
        {
            if (File.Exists(fileToRead))
            {
                using (StreamReader reader = new StreamReader(fileToRead))
                {
                    string lineOfData = default(string);
                    if ((lineOfData = reader.ReadLine()) != null)
                    {
                        var rr = lineOfData.Trim();
                    }
                }
            }
        }
    }


:)
 
Share this answer
 
Comments
smilerP 2-Aug-11 6:07am    
Thanks you very much. Your solution is very comprehensible.
 
Share this answer
 
Comments
Manas Bhardwaj 1-Aug-11 12:17pm    
I don't think this was that BAD as answer to be downvoted by a Platinum member. :O

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