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;
        }
    }
5
  • "Here i get error" what error? Commented Nov 2, 2020 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. Commented Nov 2, 2020 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 Commented Nov 2, 2020 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. Commented Nov 2, 2020 at 13:40
  • Does this answer your question? What is a NullReferenceException, and how do I fix it? Commented Nov 2, 2020 at 14:24

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok. I think i understand. So whats the correct way to pass a new value in this scenario?
via the Pop.Pop constructor, as an additional argument.
Ok that makes sense. i guess i cant do _water = new Water(8000); because that will create a new instance?
I recommend reading this page about passing by ref/value: jonskeet.uk/csharp/parameters.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.