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

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_43941252/article/details/85008529

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:“未将对象引用设置到对象的实例。”
怎么处理?

展开阅读全文

没有更多推荐了,返回首页