Blake Engquist
Blake Engquist

Reputation: 47

Receiving a null error "Unhandled Exception: System.NullException: Value cannot be null" C#

Hello i am fairly new in the programming world and im having a little problem with a simple program. I created an array and passed it through a constructor to a class named "Temperature." Im trying to find the max number of the array and return it to the method but it seems that it has no value, therefore causing the error.

namespace TempApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Temperature aTemperatureObject = new Temperature();
            double[] temp = new double [7];
            string inValue;

            for (int i = 0; i < temp.Length; i++)
            {
                Console.Write("Enter the temperature for Day {0}: ", i + 1);
                inValue = Console.ReadLine();
                temp[i] = double.Parse(inValue);
            }

            double highestTemp = aTemperatureObject.GetHighestTemp();
            double lowestTemp = aTemperatureObject.GetLowestTemp();
            double averageTemp = aTemperatureObject.GetAverageTemp();

            DisplayResults(highestTemp, lowestTemp, averageTemp);
        }

        static void DisplayResults(double high, double low, double avg)
        {
            Console.Write("Highest Temp: " + high);
            Console.Write("Lowest Temp: " + low);
            Console.Write("Average Temp: " + avg);
        }
    }
}



namespace TempApp
{
    class Temperature
    {
        private double[] temp;

        public Temperature()
        {

        }

        public Temperature(double[] temperature)
        {
            temp = temperature;
        }

        public double GetHighestTemp()
        {
            double highestTemp = temp.Max();
            return highestTemp;
        }

        public double GetLowestTemp()
        {
            double lowestTemp = temp.Min();
            return lowestTemp;
        }

        public double GetAverageTemp()
        {
            double averageTemp = temp.Average();
            return averageTemp;
        }
    }
}

Upvotes: 2

Views: 92

Answers (2)

Dmitriy Khaykin
Dmitriy Khaykin

Reputation: 5258

Your Temperature object has 2 constructors, the first is a default parameterless constructor, and the second one is the one that takes the parameter and sets it to the temp field.

You're using the parameterless constructor:

Temperature aTemperatureObject = new Temperature();
double[] temp = new double [7];

There is no place in your code where you actually pass the above array to the aTemperatureObject object.

That's why you're getting the null reference exception.

Change your code to use the other constructor and it should solve the problem. Note that in order to do so, you have to declare the array before you can instantiate the Temperature class:

double[] temp = new double [7];
Temperature aTemperatureObject = new Temperature(temp);

Upvotes: 1

Martheen
Martheen

Reputation: 5580

You store the results in temp[], yet you never insert temp[] inside aTemperatureObject. Simplest fix, remove the Temperature aTemperatureObject = new Temperature(); initializer, insert Temperature aTemperatureObject = new Temperature(temp); after the for loop.

Upvotes: 1

Related Questions