C#窗体程序导入文件老是出现“未将对象引用设置到对象的实例”,该怎么解决?(下面为代码)

qq_41385415 2017-12-13 08:02:53
namespace 图形编程实验
{
public partial class Form1 : Form
{
struct MeasPoint
{
public string Name;
public double x;
public double y;
}
private double m_MaxX, m_MaxY, m_MinX, m_MinY;
private Rectangle m_rect;
private double m_scale;
private List<MeasPoint> m_ptlst;

public Form1()
{
InitializeComponent();
m_ptlst = new List<MeasPoint>();
}

private double CalcScale()
{
double ds = 1.0;
double dsx, dsy;
if ((m_MaxX - m_MinX != 0) && (m_MaxY - m_MinY != 0))
{
dsx = Math.Abs((m_MaxX - m_MinX) / m_rect.Height);
dsy = Math.Abs((m_MaxY - m_MinY) / m_rect.Width);
ds = Math.Max(dsx, dsy);
}
else
{
if (m_MaxX - m_MinX != 0)
{
ds = Math.Abs((m_MaxX - m_MinX) / m_rect.Height);
}
else
{
if (m_MaxY - m_MinY != 0)
{
ds = Math.Abs((m_MaxY - m_MinY) / m_rect.Width);
}
else
{
ds = 1;
}
}
}
return ds;
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openDlg = new OpenFileDialog();
//指定打开文本文件(后缀名为cor)
openDlg.Filter = "武大科傻(cor)文件|*.cor|所有文件(*.*)|*.*";
DialogResult drt = openDlg.ShowDialog();
if (drt == DialogResult.OK)
{
string sFileName = openDlg.FileName;
try
{
if (m_ptlst!= null)
m_ptlst.Clear();

FileStream file = new FileStream(sFileName, FileMode.Open);
StreamReader sr = new StreamReader(file, Encoding.Default);
string st = sr.ReadLine();
int ic = 0;
while (st!= null)
{
ic++;
if (ic <= 4)
{
st = sr.ReadLine();
continue;
}
}
//切割字符串
StringSplit spt = new StringSplitFun(" ");
spt.Duplicates = false;
string[] sts = spt.Splitting(st);
int icols = sts.Count();
if (icols != 5)
{
st = sr.ReadLine();

}
MeasPoint pt = new MeasPoint();
pt.Name = sts[0];
pt.x = double.Parse(sts[1]);
pt.y = double.Parse(sts[2]);
m_ptlst.Add(pt);
st = sr.ReadLine();
sr.Close();
file.Close();
m_MaxX = 0.0;
m_MaxY= 0.0;
m_MinX = 0.0;
m_MinY = 0.0;
GetMaxMinXY(ref m_MaxX, ref m_MaxY, ref m_MinX, ref m_MinY);
m_rect = pictureBox1.ClientRectangle;
m_scale = CalcScale();
pictureBox1.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void GetMaxMinXY(ref double dmaxX, ref double dmaxY, ref double dminX, ref double dminY)
{
if (m_ptlst.Count <= 0)
return;
dmaxX = -1.0;
dmaxY = -1.0;
dminX = 1000000000;
dminY = 1000000000;
foreach (MeasPoint pt in m_ptlst)
{
if (dmaxX < pt.x)
dmaxX = pt.x;
if (dmaxY < pt.y)
dmaxY = pt.y;
if (dminX > pt.x)
dminX = pt.x;
if (dminY > pt.y)
dminY = pt.y;
}
}

private void pictureBox1_Paint_1(object sender, PaintEventArgs e)
{
if (m_ptlst == null)
return;
if (m_ptlst.Count <= 0)
return;
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Brush bush = new SolidBrush(Color.Red);
Font myFont = new Font("宋体", 8, FontStyle.Bold);
foreach (MeasPoint pt in m_ptlst)
{
double dx = (pt.y - m_MinY) / m_scale;
double dy = m_rect.Height - (pt.x - m_MinX) / m_scale;
int ix = (int)dx - 3;
int iy = (int)dy - 3;
Point[] point = new Point[3];
point[0] = new Point(ix, iy - 3);
point[1] = new Point(ix - 3, iy + 3);
point[2] = new Point(ix + 3, iy + 3);
g.FillPolygon(bush, point);
g.DrawString(pt.Name, myFont, bush, ix - 3, iy + 5);
}

}
...全文
2442 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
子阳123 2018-08-14
  • 打赏
  • 举报
回复
先看一下有没有读到这个文件,文件里是不是有东西,有没有报错,是运行报错,还是没运行就报错,这个错误有可能是对象为空,也可能是没有引用正确的命名空间
乐扣留 2018-08-13
  • 打赏
  • 举报
回复
Form2 form2 = new Form2();
Application.Run(form2);
k. 2017-12-14
  • 打赏
  • 举报
回复
打断点先找到在哪出的异常`
exception92 2017-12-14
  • 打赏
  • 举报
回复
先去学会调试程序,找到出错行。http://www.cnblogs.com/alexis/archive/2010/11/06/1870519.html 一般这种错误是由于某个对象为Null,进而又使用了它的属性或者方法,使用对象之前判断一下。
qq_41385415 2017-12-14
  • 打赏
  • 举报
回复
运行到下面程序的时候 ,显示 m_ptlst的值为Count=0,其他的file和st的值都是null?????
if (drt == DialogResult.OK)
{
string sFileName = openDlg.FileName;
try
{
if ( m_ptlst != null)
m_ptlst.Clear();

FileStream file = new FileStream(sFileName, FileMode.Open);
StreamReader sr = new StreamReader(file, Encoding.Default);
string st = sr.ReadLine();
int ic = 0;
while (st!= null)
{
ic++;
if (ic <= 4)
{
st = sr.ReadLine();
continue;
}
}

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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