0

好的,我很确定我的代码都是正确的,所以我不确定为什么会出现这个错误。:P 这是我的代码 -->

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Calculator
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());  //This is where I am getting the error at
        }
    }
}

那在我的 program.cs 文件中。这是我的 Form1.cs 文件中的内容。-->

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void btn0_Click(object sender, EventArgs e)
        {
            txtbox.Text += btn0.Text;
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            txtbox.Text += btn1.Text;
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            txtbox.Text += btn2.Text;
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            txtbox.Text += btn3.Text;
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            txtbox.Text += btn4.Text;
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            txtbox.Text += btn5.Text;
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            txtbox.Text += btn6.Text;
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            txtbox.Text += btn7.Text;
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            txtbox.Text += btn8.Text;
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            txtbox.Text += btn9.Text;
        }

        private void btnPoint_Click(object sender, EventArgs e)
        {
            if (txtbox.Text == "0")
            {
                txtbox.Text = "0.";
            }
            else
            {
                txtbox.Text += btnPoint.Text;
            }
        }

        private void btnPlus_Click(object sender, EventArgs e)
        {
            //Add plus code here
        }

        private void btnMinus_Click(object sender, EventArgs e)
        {
            //Add minus code here
        }

        private void btnTimes_Click(object sender, EventArgs e)
        {
            //Add times code here
        }

        private void btnDivide_Click(object sender, EventArgs e)
        {
            //Add divide code here
        }

        private void btnEqual_Click(object sender, EventArgs e)
        {
            //Add equal code here
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtbox.Clear();
        }
    }
}

当我尝试输入一个数字然后输入一个小数时会发生错误。例如,我按“2”,然后按“.”。会发生错误“对象引用未设置为对象的实例”。弹出。感谢您的帮助,谢谢。对不起,如果这是一个愚蠢的错误。:P

4

1 回答 1

0

在对您的问题的评论中已经为您提供了一些有用的想法,因此我将尝试在此处汇总最首选的分析路线并添加我自己的评论:

首先,发生异常的那一行 ( Application.Run(new Form1());) 并不是寻找 bug 的真正位置。Application.Run 是一种 WinForm 基础结构方法,它可以发挥一些作用,然后运行您的代码。

其次,找出问题所在的最佳方法是在 btnPoint_Click 方法中设置断点。我假设您是 WinForm 和 Visual Studio 的新手,所以我的建议是在 youtube 中搜索“Winform Debug”——有很多视频展示了如何做到这一点。基本上,这个想法是用鼠标单击您要进入的行的左侧(或者如果它是像您的情况一样的方法声明,则在下面的一行),然后通过按 F5 从 Visual Studio 中运行您的应用程序。这将使 Visual Studio 能够自动附加到您的 WinForm 进程,并让您在运行时查看变量。这将允许您逐行继续(单击 F10)并导航到代码中发生异常的确切位置。

最后,因为单击“。”时会发生这种情况。按钮,唯一可能导致此类异常的两个变量是txtboxbtnPoint,如果其中一个为 null - 就是这样。如果在调试期间将鼠标悬停在变量名称上(按照上面的说明),您应该看到变量的内容是否为空(null 将简单地表示“空”)。

于 2014-03-02T15:30:08.427 回答