なぜこれが起こり続けるのか理解できません。私は初心者ですが、私にはインスタンスへの参照セットがあります。最初は、クラスに配列のサイズを設定するのに苦労しましたが、現在は 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;
}