0

I don't think that this is a duplicate, because the situations on the general text written about null reference exceptions talk about situations in which either the instance was not initialized or it was dereferenced and the programmer didn't realize it. In this case, there are 4 variables of the object which are used with the initialized instance and all are "working" fine(which I think proves that the object is not set to null and is referenced. But a forth variable of the same instance is throwing the exception. I'm studying C# with Head First and I built a Program in which a Property is used to set a "read-only" public variable from another class. The class in which the Property is defined has a defined constructor. When I run the program, I'm getting the 'NullReference Exception unhandled" message.

The property is this:

public decimal Cost
        {
            get
            {
                decimal totalCost = CalculateCostOfDecorations();
                totalCost += ((CalculateCostOfBeveragesPerPerson() +  CostOfFoodPerPerson) * NumberOfPeople);
                if (HealthyOption)
                {
                    totalCost *= .95M;
                    return totalCost;

                }
                return totalCost;
            }
        }

The constructor is:

public DinnerParty(int numberOfPeople, bool healthyOption, bool fancyDecorations)
        {
            NumberOfPeople = numberOfPeople;
            HealthyOption = healthyOption;
            FancyDecorations = fancyDecorations;

        }

The strange thing is that in the Form.cs class where the object "dinnerParty" is initialized, all the variables from the constructor are used and assigned, no exception appeared because of any of them, but the "Cost" variable in:

private void DisplayDinnerPartyCost()
        {
            decimal Cost = dinnerParty.Cost;
            label1.Text = Cost.ToString("c");
        }

which is part of the same dinnerParty initialized instance and is suggested by Intellisense when I type the instance name, is throwing the exception. I read in microsoft's documentation that this could be caused even if the object was in fact initialized, if it is set to null, but the code that passes the value to the instance variable should be the one assigning the "null" value in this case. But since I didn't declare any nullable variables, how can this be happening?

NeoTrader
  • 119
  • 9
  • 2
    Attach a debugger. It's likely the getter code is throwing a null reference exception. – Rob Feb 24 '17 at 01:29
  • I solved it. The display – NeoTrader Feb 24 '17 at 01:47
  • Please [edit] to provide [MCVE] - doing so will either show where the error is or at least allow to re-open this post if needed. Claiming that it is not duplicate is not enough. If you already solved it - feel free to delete (or just do nothing and let this post disappear in a month). – Alexei Levenkov Feb 24 '17 at 01:49
  • I mistakenly pressed the "add comment" button on my comment. I solved it. The statement that calls DisplayDinnerPartyCost() was being called before the statement that initializes the dinnerParty object. That is why it was throwing the exception. I hope that next time the person that marks a question as a duplicate at least reads the question before doing so. Sending a general text about "Null Reference exception" is not actually helpful. As I said, before asking the question here, I read microsoft's documentation about this exception and couldn't find the answer. My question was specific. – NeoTrader Feb 24 '17 at 02:02
  • If people begin to simply throw general texts about a subject when someone asks a specific question, like I did, then there wouldn't be much use for a place like this. Since I could just pick a Textbook about C# and just read all about it in a general way. After searching about the problem and thinking about it conceptually, I couldn't find the answer and that's why I asked here. – NeoTrader Feb 24 '17 at 02:05
  • 1
    @NeoTrader You have to understand though that we get thousands of these questions. And the solution is almost always: "Run a debugger and find where the null reference is coming from.". We don't wan't to pollute StackOverflow with these questions where the answer is essentially the same. That's why these type of questions are almost immediately closed and linked to the other question. – Ilian Feb 24 '17 at 03:01

0 Answers0