0

This morning I created a windows form project to make a calculator from what I've learned and to self learn myself some programming.

Everything was fine , I could make the number buttons print the specific number in the text box , and the operator buttons clears the text box so you can write the second number , but when I press equal"=" I get this run time error :

Value cannot be null. Parameter name: String

And I will show you the code in the form1 class (as usual its the only class I have to edit) :

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    List<string> theNumberList = new List<string>();

    public string numbers = null;

    int number1, number2;
    string num1, num2, operat;

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        theNumberList.Add("1");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    public static void ShowNumbers(List<string> theList, string aString, TextBox textBox)
    {
        foreach(string number in theList)
        {
            aString = aString + number;
            textBox.Text = aString;
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        theNumberList.Add("2");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        theNumberList.Add("3");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void button4_Click(object sender, EventArgs e)
    {
        theNumberList.Add("4");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void button5_Click(object sender, EventArgs e)
    {
        theNumberList.Add("5");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void button6_Click(object sender, EventArgs e)
    {
        theNumberList.Add("6");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void button7_Click(object sender, EventArgs e)
    {
        theNumberList.Add("7");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void button8_Click(object sender, EventArgs e)
    {
        theNumberList.Add("8");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void button9_Click(object sender, EventArgs e)
    {
        theNumberList.Add("9");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void button0_Click(object sender, EventArgs e)
    {
        theNumberList.Add("0");
        ShowNumbers(theNumberList, numbers, textBox1);
    }

    private void Plus_Click(object sender, EventArgs e)
    {
        num1 = numbers;
        theNumberList.Clear();
        num2 = numbers;
        operat = "+";
    }

    private void Subtract_Click(object sender, EventArgs e)
    {

    }

    private void Multiply_Click(object sender, EventArgs e)
    {

    }

    private void Divide_Click(object sender, EventArgs e)
    {

    }

    private void Equal_Click(object sender, EventArgs e)
    {
        int result;
        switch (operat)
        {
            case "+":
                number1 = int.Parse(num1);
                number2 = int.Parse(num2);
                result = number1 + number2;
                theNumberList.Clear();
                textBox1.Text = result.ToString();
                break;
        }
    }
}

so this is the code and the error comes at Equal_click in "number1 = int.parse(num1);" here :

private void Equal_Click(object sender, EventArgs e)
    {
        int result;
        switch (operat)
        {
            case "+":
                number1 = int.Parse(num1);
                number2 = int.Parse(num2);
                result = number1 + number2;
                theNumberList.Clear();
                textBox1.Text = result.ToString();
                break;
        }
    }

I don't know how to fix it and what is wrong ?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 1
    Please make your sample smaller - check http://sscce.org/ for guidance. Also avoid "I'm new here", "thank you" and similar text not directly related to your problem. – Alexei Levenkov Sep 18 '13 at 21:27
  • The compiler is right the value is null, num1 = numbers but where do you assign a value to numbers ? – Transcendent Sep 18 '13 at 21:27

3 Answers3

1

You are getting the exception because num1 is null. In this function:

public static void ShowNumbers(List<string> theList, string aString, TextBox textBox)
{
    foreach(string number in theList)
    {
        aString = aString + number;
        textBox.Text = aString;
    }
}

you are setting the value of aString, but not numbers. See this answer for why passing the string this way does not work. You need to change it to use the numbers variable. Note we also make it a member function instead of static:

public void ShowNumbers()
{
    foreach(string number in theNumberList)
    {
        numbers = numbers + number;
        textBox1.Text = numbers;
    }
}

Also, there is another issue. You need to assign num2 when you press = not when you press +.

Community
  • 1
  • 1
lukegravitt
  • 892
  • 4
  • 12
0

It means your num1 is most likely empty and it cant convert the empty string to a number.

Ryan Schlueter
  • 2,223
  • 2
  • 13
  • 19
0

int.Parse can parse Strings which are in numeric mode like => "9" so when num1 is null or num1 = "" ; int.parse, can't parse it to int

try this

instead of Parse you should use TryParse

int.TryParse(string,out int)
mohammad jannesary
  • 1,811
  • 1
  • 26
  • 27