日期:2014-05-18  浏览次数:20604 次

求问此段代码有何问题?
C# code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace HaveATry
{
    public partial class Form1 : Form
    {
        ArrayList points;
        Point currentPoint;
        Pen thepen;
        float penwidth;
        SolidBrush thebrush;
        Color thecolor;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Point[] ps=(Point[])points.ToArray(typeof(Point));
            if (radioButton1.Checked)
                g.DrawLine(Pens.Black, ps[0], ps[ps.Length - 1]);
            else if (radioButton2.Checked)
                g.DrawRectangle(thepen, ps[0].X, ps[0].Y, ps[ps.Length - 1].X - ps[0].X, ps[ps.Length - 1].Y - ps[0].Y);
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (currentPoint.X != e.X || currentPoint.Y != e.Y)
            {
                currentPoint.X = e.X;
                currentPoint.Y = e.Y;
                points.Add(currentPoint);
                this.Invalidate();
                this.Update();
            }
        }
    }
}



报错的是这一行
  Point[] ps=(Point[])points.ToArray(typeof(Point));
说:未将对象引用设置到对象的实例
求问应该如何处理?

------解决方案--------------------
points压根没有赋值过嘛!在构造函数里加上:

points = new ArrayList();
------解决方案--------------------
ArrayList points;
Point currentPoint;
Pen thepen;
float penwidth;
SolidBrush thebrush;
Color thecolor;

你都没有初始化值!!——即使你初始化了默认值,你根本就没有地方赋值!!
你获取一个空数据,空集合 为了什么??