1. 程式人生 > >c# 未將物件引用設定到物件的例項 使用“new”關鍵字建立物件例項。在呼叫方法前通過檢查確定物件是否為null

c# 未將物件引用設定到物件的例項 使用“new”關鍵字建立物件例項。在呼叫方法前通過檢查確定物件是否為null

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.OleDb; using System.Data.SqlClient; using System.Drawing.Drawing2D; using System.Threading.Tasks;

namespace WindowsFormsApplication1 {     public partial class Form1 : Form     {                  public Form1()         {             InitializeComponent();         }

        public static DataSet getData()         {             //開啟檔案             OpenFileDialog file = new OpenFileDialog();             file.Filter = "Excel(*.xlsx)| * .xlsx | Excel(*.xls)| * .xls";             file.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);             file.Multiselect = false;             if (file.ShowDialog() == DialogResult.Cancel)                 return null;             //判斷檔案字尾             var path = file.FileName;             string fileSuffix = System.IO.Path.GetExtension(path);             if (string.IsNullOrEmpty(fileSuffix))                 return null;             using (DataSet ds = new DataSet())             {                 //判斷的Excel檔案是2003版本還是2007年版本                 string connString = "";                 if (fileSuffix == ".xls")                     connString = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";" + ";Extended Properties= \"Excel 8.0; HDR = YES; IMEX = 1 \"";                 else                     connString = "Provider = Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + path + ";" + ";Extended Properties= \"Excel 12.0; HDR = YES; IMEX = 1 \"";                 //讀取檔案                 string sql_select = "SELECT * FROM [Sheet1$]";                 using (OleDbConnection conn = new OleDbConnection(connString))                 using (OleDbDataAdapter cmd = new OleDbDataAdapter(sql_select, conn))                 {                     NewMethod1(conn);                     cmd.Fill(ds);                 }                 if (ds == null || ds.Tables.Count <= 0) return null;                 return ds;             }         }         private static void NewMethod1(OleDbConnection conn)         {             conn.Open();         }

        private void button1_Click(object sender, EventArgs e)         {             dataGridView1.DataSource = null; //每次開啟清空內容

            dataGridView1.DataSource = NewMethod().Tables[0];                      }

        private DataSet NewMethod()         {             return getData();         }

        private void button2_Click(object sender, EventArgs e)         {             Form2 frm2=new Form2();

            frm2.frm2Para = dataGridView1.DataSource;

            frm2.Show();

        }

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading.Tasks; using System.Data.OleDb; using System.Data.SqlClient; using System.Drawing.Drawing2D;

namespace WindowsFormsApplication1 {     public partial class Form2 : Form     {             public Form2()         {             InitializeComponent();         }

        private Object _parameter = null;

        public Object frm2Para         {             get { return _parameter; }             set { _parameter = value; }         }

        private void CreateImage()         {             DataSet ds = frm2Para as DataSet;                        int i = 0;             i = ds.Tables[0].Rows.Count;(錯誤提示)             double[] a = new double[i];             i = 0;             foreach (DataRow Row in ds.Tables[0].Rows)             {                 a[i] = Convert.ToDouble(Row["FAI 17"]);                 Console.WriteLine("{0}", a[i]);                 i++;                 //string str = ds.Tables[0].Rows[i]["FAI 17"].ToString();  //Rows[i]["col1"]表示i行"col1"欄位                 }

            double[] b = a.Where(p => p < 0.01 & p >= 0.0097).ToArray();             double[] c = a.Where(p => p < 0.02 & p >= 0.01).ToArray();             double[] d = a.Where(p => p < 0.03 & p >= 0.02).ToArray();             double[] e = a.Where(p => p < 0.04 & p >= 0.03).ToArray();             double[] f = a.Where(p => p < 0.05 & p >= 0.04).ToArray();             double[] h = a.Where(p => p < 0.06 & p >= 0.05).ToArray();             double[] j = a.Where(p => p < 0.07 & p >= 0.06).ToArray();             double[] k = a.Where(p => p < 0.08 & p >= 0.07).ToArray();             double[] l = a.Where(p => p < 0.09 & p >= 0.08).ToArray();             double[] m = a.Where(p => p < 0.10 & p >= 0.09).ToArray();             double[] n = a.Where(p => p < 0.11 & p >= 0.10).ToArray();             double[] o = a.Where(p => p < 0.12 & p >= 0.11).ToArray();             double[] q = a.Where(p => p < 0.13 & p >= 0.12).ToArray();             double[] r = a.Where(p => p <= 0.5416 & p >= 0.13).ToArray();

            int count1 = b.Length;             int count2 = c.Length;             int count3 = d.Length;             int count4 = e.Length;             int count5 = f.Length;             int count6 = h.Length;             int count7 = j.Length;             int count8 = k.Length;             int count9 = l.Length;             int count10 = m.Length;             int count11 = n.Length;             int count12 = o.Length;             int count13 = q.Length;             int count14 = r.Length;

            int width = 515, height = 510;              //宣告寬和高             Bitmap bitmap = new Bitmap(width, height); //建立一個bitmap物件             Graphics g = Graphics.FromImage(bitmap);   //建立graphics物件              try             {                 g.Clear(Color.White);                   //使用clear方法使畫布為白色                 //建立3個brush物件,用於填充顏色                 Brush brush1 = new SolidBrush(Color.White);                 Brush brush2 = new SolidBrush(Color.Black);                 Brush brush3 = new SolidBrush(Color.Red);                 Brush brush4 = new SolidBrush(Color.Green);                 //建立2個font物件,用於設定字型                 Font font1 = new Font("Courier New", 16, FontStyle.Bold);                 Font font2 = new Font("Courier New", 10);                 g.FillRectangle(brush1, 0, 0, width, height);      //繪製背景圖                 g.DrawString("資料分佈", font1, brush2, new Point(177, 20));     //繪製標題                 //設定座標                 Point p1 = new Point(162, 50);                 Point p2 = new Point(295, 50);                 g.DrawLine(new Pen(Color.Black), p1, p2);        //繪製直線                 //繪製文字                 g.DrawString("0.0097~0.01:", font2, brush2, new Point(14, 70));                 g.DrawString("0.01~0.02:", font2, brush2, new Point(30, 95));                 g.DrawString("0.02~0.03:", font2, brush2, new Point(30, 120));                 g.DrawString("0.03~0.04:", font2, brush2, new Point(30, 145));                 g.DrawString("0.04~0.05:", font2, brush2, new Point(30, 170));                 g.DrawString("0.05~0.06:", font2, brush2, new Point(30, 195));                 g.DrawString("0.06~0.07:", font2, brush2, new Point(30, 220));                 g.DrawString("0.07~0.08:", font2, brush2, new Point(30, 245));                 g.DrawString("0.08~0.09:", font2, brush2, new Point(30, 270));                 g.DrawString("0.09~0.10:", font2, brush2, new Point(30, 295));                 g.DrawString("0.10~0.11:", font2, brush2, new Point(30, 320));                 g.DrawString("0.11~0.12:", font2, brush2, new Point(30, 345));                 g.DrawString("0.12~0.13:", font2, brush2, new Point(30, 370));                 g.DrawString("0.13~0.5416:", font2, brush2, new Point(14, 395));                 //繪製柱形圖                 g.FillRectangle(brush3, 120, 70, count1, 17);                 g.FillRectangle(brush3, 120, 95, count2, 17);                 g.FillRectangle(brush3, 120, 120, count3, 17);                 g.FillRectangle(brush3, 120, 145, count4, 17);                 g.FillRectangle(brush3, 120, 170, count5, 17);                 g.FillRectangle(brush3, 120, 195, count6, 17);                 g.FillRectangle(brush3, 120, 220, count7, 17);                 g.FillRectangle(brush3, 120, 245, count8, 17);                 g.FillRectangle(brush3, 120, 270, count9, 17);                 g.FillRectangle(brush3, 120, 295, count10, 17);                 g.FillRectangle(brush3, 120, 320, count11, 17);                 g.FillRectangle(brush3, 120, 345, count12, 17);                 g.FillRectangle(brush3, 120, 370, count13, 17);                 g.FillRectangle(brush3, 120, 395, count14, 17);

                //繪製所有區間的數量顯示                 //g.DrawRectangle(new Pen(Color.Green), 10, 210, 280, 80);    //繪製範圍框                 g.DrawString(count1.ToString() + "個", font2, brush4, new Point(300, 70));                 g.DrawString(count2.ToString() + "個", font2, brush4, new Point(300, 95));                 g.DrawString(count3.ToString() + "個", font2, brush4, new Point(300, 120));                 g.DrawString(count4.ToString() + "個", font2, brush4, new Point(300, 145));                 g.DrawString(count5.ToString() + "個", font2, brush4, new Point(300, 170));                 g.DrawString(count6.ToString() + "個", font2, brush4, new Point(300, 195));                 g.DrawString(count7.ToString() + "個", font2, brush4, new Point(300, 220));                 g.DrawString(count8.ToString() + "個", font2, brush4, new Point(300, 245));                 g.DrawString(count9.ToString() + "個", font2, brush4, new Point(300, 270));                 g.DrawString(count10.ToString() + "個", font2, brush4, new Point(300, 295));                 g.DrawString(count11.ToString() + "個", font2, brush4, new Point(300, 320));                 g.DrawString(count12.ToString() + "個", font2, brush4, new Point(300, 345));                 g.DrawString(count13.ToString() + "個", font2, brush4, new Point(300, 370));                 g.DrawString(count14.ToString() + "個", font2, brush4, new Point(300, 395));

                pictureBox1.Image = bitmap;             }             catch (Exception ex)             {                 MessageBox.Show(ex.Message);             }         }

        private void Form2_Load(object sender, EventArgs e)         {             CreateImage();         }

            } }  

我用form1連線到excel並讀取資料,form2獲取讀取到的資料並設定區間對資料進行分類處理,然後生成柱形圖。但是我不知道為什麼標註的紅色部分報錯,網上查了原因,但是我不知道該怎麼修改,求大神幫忙