0

Here is my code which is very simple just for solving the problem. I can't find it myself. I think there is something wrong with my classes.

   using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    public class Time
    {
        public string Hour { get; set; }
        public string Min { get; set; }


    }

    public class Car
    {
        public Time Clock { get; set; }


        public void CarTime()
        {
            Clock.Min="3"; // here 
        }

    }

}

Here is my main which calls the cartime function.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            Car car = new Car();
            car.CarTime();
        }
    }
}

It is not working because I get null reference exception. I don't know why. Sorry for my English.

Alex
  • 146
  • 9
  • 3
    You haven't set `Clock` to anything. Somewhere you need to set `Clock` to a `new Time()` – Sean May 01 '20 at 08:29
  • 1
    done. It is working now. Thank you. – Alex May 01 '20 at 08:32
  • 1
    The duplicate contains all the informations needed. In basic terms, to work with normal class (non static) you need an _instance_ of that class. The instance is create when you call _new ClassName_ as you do for the Car class where _car_ (the variable) contains the instance to work with. You need to do that as well for the Time class – Steve May 01 '20 at 08:33

0 Answers0