0

So, First of all, I'm a beginner, and 6 months of experience in C# at school. I can do it the other way, but I want to know more ways to do stuffs so.. And I need help about this. What happens is, when i run the program everything goes okay until private void Add(), some error comes up and says that Simplify is null? Thanks in advance.

using System;

namespace test1
{
 class Details
 {
    int NumberSimplify;
    double Solution;

    private void Ask()
    {
        Console.Write("Would you like to Add or Substract (1 - Add, 2 - Substract) -► ");
        int Opt = int.Parse(Console.ReadLine());

        switch(Opt)
        {
            case 1:
                Console.Write("\nHow many numbers would you like to add? -► ");
                NumberSimplify = int.Parse(Console.ReadLine());                    
                break;

            case 2:
                break;
        }
    }

    public void DisplayResult(double[] Simplify)
    {
        Console.Write("Result from Addition -► ", Add(Simplify));
    }

    private void AddDetails()
    {
        Ask();
        double[] Simplify = new double[NumberSimplify];
        Console.WriteLine();
        for (int i = 0; i < Simplify.Length; i++)
        {
            Console.Write("Number {0} -► ", i + 1);
            Simplify[i] = double.Parse(Console.ReadLine());
        }
    }



    private double Add(double[] Simplify)
    {
        AddDetails();
        for (int i = 0; i < Simplify.Length; i++)
        {
            Solution = Simplify[i] + Simplify[i + 1];
        }
        return Solution;
    }


}


class Execute
{
    public static double[] Simplify { get; private set; }

    static void Main(string[] args)
    {

        Details AddOrSub = new Details();
        AddOrSub.DisplayResult(Simplify);
        Console.ReadLine();

    }
}

}

dotke
  • 13
  • 4

1 Answers1

3

Simplify is indeed null....

public static double[] Simplify { get; private set; } 

You need to initialize arrays... so this should be more like;

public static double[] Simplify { get; private set; } = new double[someNumberOfElements]

But as pointed out in the comments, you dont even use this initial version of the variable (It is overwritten anyway in your next methods) so consider removing it altogether and re-visiting your control flow...

Milney
  • 6,253
  • 2
  • 19
  • 33