Visual Studio错误System.Windows.Forms.dll中发生未处理的“System.ArgumentNullException”类型异常

时间:2017-06-22 19:18:39

标签: c# visual-studio

我的c#windows窗体应用程序出错了,这个错误只发生在Windows 10上,程序在Windows 7中运行正常。 这是代码中无法正常工作的部分。 Visual Studio版本:社区2015 感谢。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    public struct Mypizzas
    {
        public string mypizzaname;
        public float mypizzaprice;
    }
    public struct Mypizzasordered
    {
        public string mypizzaname;
        public float mypizzaprice;
        public float QtyOrdered;

    }
    string newpizza;
    Mypizzasordered[] Orderlist = new Mypizzasordered[51];
    Single mytotalprice;
    string numpizzasordered;

    int countertest;
    int Mycounter;


    private void Form1_Load(object sender, EventArgs e)
    {

        Mypizzas[] myarrayofpizzas = new Mypizzas[6];
        myarrayofpizzas[0].mypizzaname = ("Hawaiian");
        myarrayofpizzas[0].mypizzaprice = 3.99f;
        myarrayofpizzas[1].mypizzaname = "Margherita";
        myarrayofpizzas[1].mypizzaprice = 5.99f;
        myarrayofpizzas[2].mypizzaname = "Chicago Pizza";
        myarrayofpizzas[2].mypizzaprice = 4.99f;
        myarrayofpizzas[3].mypizzaname = "New York Style Pizza";
        myarrayofpizzas[3].mypizzaprice = 6.5f;
        myarrayofpizzas[4].mypizzaname = "Sicilian Pizza";
        myarrayofpizzas[4].mypizzaprice = 5.99f;



        for (Mycounter = 0; Mycounter <= 6; Mycounter++)
        {

            listBox1.Items.Add(myarrayofpizzas[Mycounter].mypizzaname);
            listBox2.Items.Add(myarrayofpizzas[Mycounter].mypizzaprice);
        }
    }

此行发生错误:listBox1.Items.Add(myarrayofpizzas [Mycounter] .mypizzaname);

2 个答案:

答案 0 :(得分:2)

如果这是您的实际代码,那么问题是您只使用数据填充5个项目(0-4)。因此,当您添加第6个项目(实际上是索引5)时,它对mypizzaname具有空值。这就是抛出异常的原因。

如果你添加一个额外的比萨饼,这个问题就会消失,但是你会遇到一个不同的问题,那就是你会得到一个ArgumentOutOfBounds异常,这是因为试图访问一个没有这个问题的数组索引。存在(大小为6的数组只能有0-5项,因此访问项目6实际上是第7项)

最简单的解决方法是更改​​

for (Mycounter = 0; Mycounter <= 6; Mycounter++)

for (Mycounter = 0; Mycounter < 5; Mycounter++)

for (Mycounter = 0; Mycounter <= 4; Mycounter++)

解决这个问题的另一种方法是添加额外的披萨并更换你的柜台。

myarrayofpizzas[5].mypizzaname = "New Pizza";
myarrayofpizzas[5].mypizzaprice = 5.99f;
...
for (Mycounter = 0; Mycounter < 6; Mycounter++)

答案 1 :(得分:0)

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    public struct Mypizzas
    {
        public string mypizzaname;
        public float mypizzaprice;
    }
    public struct Mypizzasordered
    {
        public string mypizzaname;
        public float mypizzaprice;
        public float QtyOrdered;

    }
    string newpizza;
    Mypizzasordered[] Orderlist = new Mypizzasordered[51];
    Single mytotalprice;
    string numpizzasordered;

    int countertest;
    int Mycounter;


    private void Form1_Load(object sender, EventArgs e)
    {

        Mypizzas[] myarrayofpizzas = new Mypizzas[6];
        myarrayofpizzas[0].mypizzaname = ("Hawaiian");
        myarrayofpizzas[0].mypizzaprice = 3.99f;
        myarrayofpizzas[1].mypizzaname = "Margherita";
        myarrayofpizzas[1].mypizzaprice = 5.99f;
        myarrayofpizzas[2].mypizzaname = "Chicago Pizza";
        myarrayofpizzas[2].mypizzaprice = 4.99f;
        myarrayofpizzas[3].mypizzaname = "New York Style Pizza";
        myarrayofpizzas[3].mypizzaprice = 6.5f;
        myarrayofpizzas[4].mypizzaname = "Sicilian Pizza";
        myarrayofpizzas[4].mypizzaprice = 5.99f;



        for (Mycounter = 0; Mycounter < 6; Mycounter++)
        {

            listBox1.Items.Add(myarrayofpizzas[Mycounter].mypizzaname);
            listBox2.Items.Add(myarrayofpizzas[Mycounter].mypizzaprice);
        }
    }

你循环7次,而不是6次。 0,1,2,3,4,5,6