0

I have a Main.class which runs a timer at 5 seconds (when it resets i want to do a bunch of methods and then repeat the timer). I have a Water.class with a currentReserve-property and a pop.class with a CurrentThirst and MaximumThirst-propertys. In the Pop class i have a method thats get called when the timer reset. Basically i want the popclass to consume the currentReserve-int in the object which i have created in the Mainclass. I provide the essential code and mark with comments where the debugger thinks iam wrong:

public AITest()
{
        public Water _water;
        public Pop _pop;
        Timer dayTimer = new Timer();
        int timeLeft = 5;

        public AITest()
        {
            InitializeComponent();

            _water = new Water(8000);
            lblWater.Text = _water.CurrentReserve.ToString();

            _pop = new Pop(100, 100);

            dayTimer.Interval = 1000;
            dayTimer.Enabled = true;
            dayTimer.Tick += dayTimer_Tick;
            dayTimer.Start();
            lblCountDown.Text = timeLeft.ToString();
        }

        private void dayTimer_Tick(object sender, EventArgs e)
        {
            lblCountDown.Text = timeLeft.ToString();
            timeLeft -= 1;

            if (timeLeft < 0)
            {
                timeLeft = 5;

                _water.CurrentReserve += 100;
                _pop.HaveToDrink();
                lblWater.Text = _water.CurrentReserve.ToString();
            }
        }
    }
}
public class Pop
{
        public int CurrentThirst { get; set; }
        public int MaximumThirst { get; set; }
        public Water _water;

        public Pop(int currentThirst, int maximumThirst)
        {
            CurrentThirst = currentThirst;
            MaximumThirst = maximumThirst;
        }

        public void HaveToDrink()
        {
            CurrentThirst -= 30;
            _water.CurrentReserve -= 30; //Here i get an error
        }
    }
public class Water
    {
        public int CurrentReserve { get; set; }

        public Water(int currentReserve)
        {
            CurrentReserve = currentReserve;
        }
    }
prajmen
  • 1
  • 3
  • "Here i get error" what error? – gilliduck Nov 02 '20 at 13:34
  • So `Pop` has an instance of `Water` but how did `Water` get in there? It's not newed up or injected in anywhere you've shown. – gilliduck Nov 02 '20 at 13:35
  • 3
    I have an issue please fix it! love this kind of question ;) it's like Visual studio has a very nice debugger but I do not want to use it :p – pix Nov 02 '20 at 13:36
  • When i tried to be more informative with my problem and pasted the debugger-error my question was autodeleted and said i would find my answers in the links provided, i read them carefully but it didnt help my problem. Im sorry. – prajmen Nov 02 '20 at 13:40
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Cee McSharpface Nov 02 '20 at 14:24

1 Answers1

2

You've got a _water member twice: once in AITest and once in the Pop class. Use only one, and pass it around. The exception you get will be a NullReferenceException, because nothing is ever assigned to the Pop._water member.

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77