Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am trying to read text files with the help of stream reader, but i got this error "An exception of type 'System.NullReferenceException' occurred in DevotionText.DLL but was not handled in user code" when i try running it in the emulator

The code below
C#
// xaml.cs
public partial class MainPage : PhoneApplicationPage
    {
        List<string> devotions = new List<string>();
        
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            AddDevotions(); 

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            DateTime dt = DateTime.Now;
            int month = dt.Month;
            int year = dt.Year;
            int index;

            if (DateTime.IsLeapYear(year) || (month <= 2))
            {
                index = dt.DayOfYear - 1; // list is indexed from 0
            }
            else
            {
                index = dt.DayOfYear; // add a day
            }

            textblock.Text = devotions[index].ToString(); // or some other property
        }

        private void AddDevotions()
        {
            for (int i = 1; i <= 366; i++)
            {
                string filePath = "Devotions/Devotion1" + i.ToString() + ".txt";
                
                devotions.Add(d);
            }
        }

        public string ReadTextFile(string textFilePath)
        {
            string text = null;

            using (StreamReader r = new StreamReader(textFilePath))
            {
                text = r.ReadToEnd();
            }

            return text;
        }

        public string d { get; set; }


Kindly help
Posted
Updated 2-Feb-15 5:27am
v2
Comments
ZurdoDev 2-Feb-15 10:58am    
Which line of code causes the error?
Member 10627743 2-Feb-15 11:10am    
The error occur on the following line " textblock.Text = devotions[index].ToString(); // or some other property"
ZurdoDev 2-Feb-15 11:21am    
That means devotions[index] is null.
Herman<T>.Instance 2-Feb-15 11:47am    
exactly or textblock is null. I don't see any declaration for textblock.
Futhermore strange code. It starts in the constructor with adding devitions before the file is read!
Member 10627743 2-Feb-15 12:35pm    
I have corrected the code but am still getting the same error, see the code below

public partial class MainPage : PhoneApplicationPage
{
List<string> devotions = new List<string>();

// Constructor
public MainPage()
{
InitializeComponent();

AddDevotions();
int index = DateTime.Now.DayOfYear;
textblock.Text = devotions[index];

// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
DateTime dt = DateTime.Now;
int month = dt.Month;
int year = dt.Year;
int index;

if (DateTime.IsLeapYear(year) || (month <= 2))
{
index = dt.DayOfYear - 1; // list is indexed from 0
}
else
{
index = dt.DayOfYear; // add a day
}

textblock.Text = devotions[index].ToString(); // or some other property
}


private void AddDevotions()
{
for (int i = 1; i <= 366; i++)
{
string filePath = "Devotions/Devotion1" + i.ToString() + ".txt";

devotions.Add(d);
}
}

public string ReadTextFile(string textFilePath)
{
string text = null;

using (StreamReader r = new StreamReader(textFilePath))
{
text = r.ReadToEnd();
}

return text;
}

public string d { get; set; }

1 solution

C#
private void AddDevotions()
{
    for (int i = 1; i <= 366; i++)
    {
        string filePath = "Devotions/Devotion1" + i.ToString() + ".txt";

        devotions.Add(d);
    }
}

Since your code never sets the auto property d to anything, every item in your devotions list will be null. When you try to call .ToString() on a null reference, you will get a NullReferenceException.

I suspect that you meant to read the file from filePath into the list? If so, try the following:
C#
private void AddDevotions()
{
    for (int i = 1; i <= 366; i++)
    {
        string filePath = Path.GetFullPath("Devotions/Devotion1" + i.ToString() + ".txt");
        
        if (File.Exists(filePath))
        {
            devotions.Add(File.ReadAllText(filePath));
        }
        else
        {
            devotions.Add(string.Format("Devotions file '{0}' was not found.", i));
        }
    }
}
 
Share this answer
 
Comments
Member 10627743 2-Feb-15 12:38pm    
When i added the code above i got the following error message "Error 1 'System.IO.File' does not contain a definition for 'ReadAllText'" kindly help
Richard Deeming 2-Feb-15 12:48pm    
The File.ReadAllText method[^] has existed since .NET 2.0; if you were using an earlier version, then you wouldn't be able to use generics (eg: List<string>).

If you're using a different framework, you'll need to specify which one.

Alternatively, replace the File.ReadAllText call with the ReadTextFile method from your question:
devotions.Add(ReadTextFile(filePath));
Member 10627743 2-Feb-15 13:02pm    
Thank you richard Deeming the content of the text file has finally displayed in the emulator

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