c#System.NullReferenceException:“未将对象引用设置到对象的实例。”

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;
using System.IO;

namespace 点名程序
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int a = int.Parse(textBox1.Text);//读取点名人数
FileData[] fileDatas = new FileData[100];//初始化数据存放数组
OpenFileDialog fileDialog = new OpenFileDialog();
if (fileDialog.ShowDialog() == DialogResult.OK)//弹出选择文件框
{
//读取文件的源路径及其读取流
string filePath = fileDialog.FileName;
StreamReader fileRead = new StreamReader(filePath);
for (int j = 0; !fileRead.EndOfStream; j++)//读取流直至文件末尾结束
{
string fileReadLine = fileRead.ReadLine();//读取每行数据
int i = fileReadLine.IndexOf(",");//读取特定字符下标
fileDatas[j].number = Convert.ToInt32(fileReadLine.Substring(0, 7));//读取学号
fileDatas[j].name = fileReadLine.Substring(i + 1);//读取名字
}
fileRead.Close();//关闭文件流
}
Random rand = new Random();//生成随机函数
int b = rand.Next(a);//随机读取数据
textBox2.Text = “”;
for (int i = 0; i < a; i++)//输出点名数据
{
textBox2.Text += fileDatas[i].number + fileDatas[i].name + “\r\n”;
}
}
}
public class FileData//储存名字和学号
{
public string name;
public int number;
}
}

System.NullReferenceException:“未将对象引用设置到对象的实例。”
怎么处理?

你可能感兴趣的:(c#)