arrays - Receiving a null error "Unhandled Exception: System.NullException: Value cannot be null" C# -
hello new in programming world , im having little problem simple program. created array , passed through constructor class named "temperature." im trying find max number of array , return method seems has no value, therefore causing error.
namespace tempapp { class program { static void main(string[] args) { temperature atemperatureobject = new temperature(); double[] temp = new double [7]; string invalue; (int = 0; < temp.length; i++) { console.write("enter temperature day {0}: ", + 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; } } }
your temperature object has 2 constructors, first default parameterless constructor, , second 1 one takes parameter , sets temp field.
you're using parameterless constructor:
temperature atemperatureobject = new temperature(); double[] temp = new double [7];
there no place in code pass above array atemperatureobject
object.
that's why you're getting null reference exception.
change code use other constructor , should solve problem. note in order so, have declare array before can instantiate temperature class:
double[] temp = new double [7]; temperature atemperatureobject = new temperature(temp);
Comments
Post a Comment