C# list .add()“未将对象引用设置到对象的实例。”

YXLYY 2015-04-23 02:59:18
首先我定义两个类,虽然这两个类不是太规范我觉得还是能用的
public class TPoint
{
public int x;
public int y;
public int z;
public TPoint()
{
x = 0;
y = 0;
z = 0;
}
public TPoint(int a, int b, int c)
{
x = a;
y = b;
z = c;
}


}
public class Surface
{
public List<TPoint> Simppoint = new List<TPoint>();
public Surface()
{

}
}
然后我从一个txt 文档你们读取数据
里面数据类似 12 45 1
22 88 1
......................
class Doc
{
public Surface[] surface = new Surface[100];
public int number;
public Doc()
{
FileStream fs = new FileStream(@"D:\a.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string tpoint;
int i = 0;
int N;

while ((tpoint = sr.ReadLine()) != null)
{

TPoint tp = new TPoint();
for (i = 0; tpoint[i] != ' '; i++)
{
tp.x = tp.x * 10 + tpoint[i] - '0';
//surface[N].point[surface[N].length].x=surface[N].point[surface[N].length].x*10+(tpoint[i]-'0');
}
for (i++; tpoint[i] == ' '; i++)
{
}
for (; tpoint[i] != ' '; i++)
{
tp.y = tp.y * 10 + tpoint[i] - '0';
//surface[N].point[surface[N].length].y=surface[N].point[surface[N].length].y*10+(tpoint[i]-'0');
}
for (i++; tpoint[i] == ' '; i++)
{
}
for (; i < tpoint.Length; i++)
{
tp.z = tp.z * 10 + tpoint[i] - '0';
//surface[N].point[surface[N].length].z=surface[N].point[surface[N].length].z*10+(tpoint[i]-'0');
}
N = tp.z;
surface[N].Simppoint.Add(tp);//每次程序运行到这里就发生中断异常
number = N;
}
sr.Close();

}
每次程序运行到这里就发生中断异常 surface[N].Simppoint.Add(tp);+ 已引发:“未将对象引用设置到对象的实例。”(System.NullReferenceException) 异常消息 = "未将对象引用设置到对象的实例。", 异常类型 = "System.NullReferenceException", 异常 WinRT 数据 = ""
真心很急,求大神指教
...全文
1497 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
YXLYY 2015-04-23
  • 打赏
  • 举报
回复
引用 3 楼 Z65443344 的回复:
确切的说,是surface[N]是null 你需要先surface[N]=new Surface(); 才行
哦哦哦我本来是用C++写,后来改成c#的,谢谢了哈
於黾 2015-04-23
  • 打赏
  • 举报
回复
确切的说,是surface[N]是null 你需要先surface[N]=new Surface(); 才行
於黾 2015-04-23
  • 打赏
  • 举报
回复
surface 没有实例化 你断点跟,只不过是定义了长度为100的空数组,里面都是null 既然已经定义了list,怎么还定义数组?是想闹哪样
YXLYY 2015-04-23
  • 打赏
  • 举报
回复
求大神指教,我觉得我给对象都实例化的

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧