c# - 对象引用未设置为实例错误

标签 c# object

我不明白为什么会这样。我是初学者,但对我来说有一个实例引用集。一开始我很难让类的数组大小现在设置为 100

这是我的代码。

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.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

    SalesmanClass[] salesmen = new SalesmanClass[100];


    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        if (textBox6.Text.Trim().Length != 0)
        {


            for (int i = 0; i <= salesmen.Length; i++)
            {

                if (salesmen[i] == null)
                {
                    salesmen[i].name = textBox6.Text; // error happens here when i enter something into the form it says
                    //Object reference not set to an instance of an object error
                    break;

                }

            }

        }
        else
        {
            MessageBox.Show("Please Input a Name");
        }

    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        List<string> names = new List<string>();

        for (int i = 0; i < salesmen.Length; i++)//var salesmen in salesmen)
        {
            names.Add(salesmen[i].Name);// same problem here
        }
        listBox1.Items.Add(names);

    }

    private void textBox6_TextChanged(object sender, EventArgs e)
    {

    }

    private void button2_Click_1(object sender, EventArgs e)
    {

    }
}
}

SalesmanClass.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
public class SalesmanClass
{

    public string name;
    public string cNum;
    public string Email;
    public string address;
    public string gArea;
    public int tSales;

    public SalesmanClass()
    {
        name = null;
        cNum = null;
        Email = null;
        address = null;
        gArea = null;


    }

最佳答案

您在 if 语句中使用了 == 运算符,这意味着如果销售员为空,则设置其名称。我相信您的意思是如果推销员为空 (!=)。您还会在 for 循环中遇到 index out of range 异常,因此我也已在此处修复该问题。

for (int i = 0; i < salesmen.Length; i++)
{

    if (salesmen[i] != null)
    {
        salesmen[i].name = textBox6.Text; // error happens here when i enter something into the form it says
        //Object reference not set to an instance of an object error
        break;

    }

}

如果您确实意味着该实例为空,也许您想要这样的东西:

for (int i = 0; i < salesmen.Length; i++)
{

    if (salesmen[i] == null)
    {
        salesmen[i] = new SalesmanClass();
        salesmen[i].name = textBox6.Text; // error happens here when i enter something into the form it says
        //Object reference not set to an instance of an object error
        break;

    }

}

在您的 button2_Click 方法中,您遇到了同样的问题,因为您没有跳过 null 销售员。您还可以看到我评论说我将您正在访问的字段从 Name 更改为 name 因为在您发布的类(class)中,没有 Name 字段。

private void button2_Click(object sender, EventArgs e)
{
    List<string> names = new List<string>();

    for (int i = 0; i < salesmen.Length; i++)//var salesmen in salesmen)
    {
        if (salesmen[i] != null)
        {
            names.Add(salesmen[i].name); // changed from .Name to .name
        }
    }
    listBox1.Items.Add(names);

}

关于c# - 对象引用未设置为实例错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16643589/

相关文章:

javascript - SignalR 问题 : Cannot connect to Hub class

c# - 动态对象智能感知

c# - 通过 Azure Active Directory 对客户端进行身份验证以连接到 ApiApp

c# - Visual Studio 中 Windows 服务和配置应用程序的最佳实践

c# - 如何从后面的 C# 代码更新 WPF 绑定(bind)的值?

c++ - 从堆栈中删除一个对象?

javascript - $.proxy, bind, call, apply 的区别

c# - 如何检查文本文件是否正在使用?

javascript - 在 'this' 和对象的原型(prototype)方法中使用 'constructor' 时如何正确返回值?

javascript - 仅对一个变量设置状态