Hi, why it's always got the error message said that "NullReferenceException Unhandled" ?

Here is my code:

public partial class MainPage : PhoneApplicationPage
    {
        private FlashCard _flashCard;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            // This could go under somewhere like a load new flash card button or
            // menu option etc.
            try
            {
                _flashCard = new FlashCard(@"D:\My Workspaces\Windows Phone 7 Solution\SimpleFlashCard\App.xaml");
            }
            catch (Exception)
            {
                // do something
            }
        }

        private void btnLeft_Click(object sender, RoutedEventArgs e)
        {
            DisplayPrevious();
        }

        private void btnRight_Click(object sender, RoutedEventArgs e)
        {
            DisplayNext();
        }

        private void DisplayNext()
        {
            FlashText.Text = _flashCard.GetNextLine();
        }

        private void DisplayPrevious()
        {
            FlashText.Text = _flashCard.GetPreviousLine();
        }
    }

and a Class named "FlashCard":

public class FlashCard
    {
        private readonly string _file;
        private readonly List<string> _lines;

        private int _currentLine;

        public FlashCard(string file)
        {
            _file = file;
            _currentLine = -1;

            // Ensure the list is initialized
            _lines = new List<string>();

            try
            {
                LoadCard();
            }
            catch (Exception ex)
            {
                // either handle or throw some meaningful message that the card
                // could not be loaded.
            }
        }

        private void LoadCard()
        {
            if (!File.Exists(_file))
            {
                // Throw a file not found exception
            }

            using (var reader = File.OpenText(_file))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    _lines.Add(line);
                }
            }
        }

        public string GetPreviousLine()
        {
            // Make sure we're not at the first line already
            if (_currentLine > 0)
            {
                _currentLine--;
            }

            return _lines[_currentLine];
        }

        public string GetNextLine()
        {
            // Make sure we're not at the last line already
            if (_currentLine < _lines.Count - 1)
            {
                _currentLine++;
            }

            return _lines[_currentLine];
        }
    }

The solution is about to made a simple Flash Card application. While I am executing the code, and hitting the 'Next' button, it always error under the "FlashText.Text = _flashCard.GetNextLine();" line said "NullReferenceException was unhandled", and so for the 'Previous' button under the line "FlashText.Text = _flashCard.GetPreviousLine();".

Please help. Thanks.

Recommended Answers

All 6 Replies

Line 19 says if you have an error loading the file, you ignore it. This may be the source of your problem. If you are going to put a 'try' block, catch an exception! Display something.

I suspect it is failing there and _flashCard (who ever told you naming variables with a prefixed _ is a good idea is an idiot) is never initialized.

Line 19 says if you have an error loading the file, you ignore it. This may be the source of your problem. If you are going to put a 'try' block, catch an exception! Display something.

I suspect it is failing there and _flashCard (who ever told you naming variables with a prefixed _ is a good idea is an idiot) is never initialized.

I've try to put an exception but it was the same.

Change line 31 to say throw new Exception("No file"); .

Once you've done that, highlight line 10 of FlashCard.cs and press F9 (set breakpoint). Press F5 to start your program. When it stops at line 10 start pressing F11. You'll set it move from line to line as it executes your code. It will jump into the LoadCard routine when you press F11 at line 18. See if it enters the if statement. Look at the values of each of the variables as it executes (point mouse at variable and it will show the current value).

You should see where it fails.

Change line 31 to say throw new Exception("No file"); .

Once you've done that, highlight line 10 of FlashCard.cs and press F9 (set breakpoint). Press F5 to start your program. When it stops at line 10 start pressing F11. You'll set it move from line to line as it executes your code. It will jump into the LoadCard routine when you press F11 at line 18. See if it enters the if statement. Look at the values of each of the variables as it executes (point mouse at variable and it will show the current value).

You should see where it fails.

@Momerath: I've tried but still. I've higlight line 10 of the class but after I run it, it's just directly goes to the same place, getting stuck on line 37 while hitting the 'Next' or 42 while hitting the 'Previous'. I try it again if there is any fault but just the same. Always said "NullReferenceException was unhandled".

So sorry, I really confused about this. I've tried another solution and it's the same.

Are you saying that it never stopped at the breakpoint I told you to set on line 10? That you watched it read each line from the file and could see that it was putting values into _list?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.