Question Unhandled exception 'System.NullReferenceException' in code

flaviu

New member
Joined
May 13, 2016
Messages
2
Programming Experience
Beginner
Please help me identify what is wrong here. It was working at first. I made some changes that did not work out, but when I reverted them, I keep getting this exception thrown and I can't tell where from. Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Problema_elevi
{
    class Elev
    {
        private int numar_matricol;
        private string nume;
        private int varsta;
        private int inaltime;
        private double media;
        private DateTime data;


        #region Proprietati
        public int Numar_matricol
        {
            get { return numar_matricol; }
            set { numar_matricol = value; }
        }


        public string Nume
        {
            get { return nume; }
            set { nume = value; }
        }


        public int Varsta
        {
            get { return varsta; }
            set { varsta = value; }
        }


        public int Inaltime
        {
            get { return inaltime; }
            set { inaltime = value; }
        }


        public double Media
        {
            get { return media; }
            set { media = value; }
        }


        public DateTime Data
        {
            get { return data; }
            set { data = value; }
        }
        #endregion
    }


    class Clasa
    {
        Elev[] clasa = new Elev[30];


        public void citire()
        {
            Console.WriteLine("Introduceti numarul de elevi");
            int n = Convert.ToInt32(Console.ReadLine());
            while (n>30)
            {
                Console.WriteLine("O clasa nu poate avea mai mult de 30 elevi. Alegeti alt numar");
                n = Convert.ToInt32(Console.ReadLine());
            }
            for (int i=0;i<n;i++)
            {
                clasa[i] = new Elev();
                Console.WriteLine("Introduceri numarul matricol al elevului");
                clasa[i].Numar_matricol = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Introduceri numele elevului");
                clasa[i].Nume = Console.ReadLine();
                Console.WriteLine("Introduceri varsta elevului");
                clasa[i].Varsta = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Introduceri inaltimea elevului in centimetri");
                clasa[i].Inaltime = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Introduceri media elevului");
                clasa[i].Media = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Introduceri data mediei in formatul zz.ll.aaaa");
                clasa[i].Data = Convert.ToDateTime(Console.ReadLine());
            }
        }


        public void afisare()
        {
            foreach (Elev x in clasa)
            {
                Console.WriteLine("Nr. Matricol: {0}; nume: {1}; varsta: {2}; inaltimea: {3}; media {4}; data mediei: {5}",
                    x.Numar_matricol, x.Nume, x.Varsta, x.Inaltime, x.Media, x.Data);
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Clasa A = new Clasa();
            A.citire();
            A.afisare();


            Console.ReadLine();
        }
    }
}
 
Last edited by a moderator:
Firstly, please use formatting tags when posting code snippets for readability. There is a button on the advanced editor to add them for you or you can type them in yourself. It looks like this:

[XCODE=c#]your code here[/XCODE]
 
As for the issue, it's actually very easy to determine where it's coming from. When (almost) any exception goes unhandled, the Exception Assistant window pops up and gives you access to all sorts of information. One of the most important is the stack trace. It provides a list of methods that were currently being executed to get to the point at which the exception was thrown. That immediately lets you hone in on the offending part of the code.

Once you know the line on which the exception was thrown, you can start looking for the reason. A NullReferenceException is thrown when you try to access a member of an object that doesn't exist, e.g.
string myString;
int stringLength = myString.Length;
In that case, no String object has been assigned to the variable so getting the Length of the String is not possible and a NullReferenceException is thrown. This particular example wouldn't actually get that far as the compiler would detect the issue but in many other cases it's not possible for the compiler to know ahead of time whether a member access will work so a run time exception results.

If you can't tell which reference is null simply by looking, debug. You can set a breakpoint on that line and, when execution breaks, you can test each reference to see which is null. Once you know which reference is null, you work backwards to determine why it's null.

Even if you can't work out the exact solution, at least you can do more for us than just dump all your code on us and expect that we'll wade through it. You can post only the relevant code and indicate exactly where the exception occurs and even specify what data was in use at the time. That means far less guesswork required, if you even need our help at all.
 
Back
Top Bottom