程序调试闪退,无法执行
两个窗口,form1和form2.form1
form2
想实现的功能是在Form2窗口中选择项后,点确定将里面的值在form1窗口的会员信息里的三个textbox里显示出来,但是点击调试程序闪了一下就关闭了,点击开始执行则显示的是未将对象引用设置到对象实例,异常文本为:
System.NullReferenceException: 未将对象引用设置到对象的实例。
在 WindowsFormsApplication1.Form1.Form1_Load(Object sender, EventArgs e) 位置 D:\WYF\毕业设计(修改)\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:行号 22
在 System.Windows.Forms.Form.OnLoad(EventArgs e)
在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
在 System.Windows.Forms.Control.CreateControl()
在 System.Windows.Forms.Control.WmShowWindow(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, I
两个窗口代码为:
form1:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form2 fv = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.Text = fv.findv[1].ToString();//这个就是异常文本里指的第22行
this.textBox3.Text = fv.findv[0].ToString();
this.textBox3.Text = fv.findv[2].ToString();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
Form2 fv = new Form2();
fv.ShowDialog();
}
}
}
form2
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public string id;
public string[] findv;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Sanck.con.Open();
SqlDataAdapter dap = new SqlDataAdapter("select * from 会员表 where 编号 like '%" + this.textBox1.Text + "%'", Sanck.con);
DataSet ds = new DataSet();
dap.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
Sanck.con.Close();
}
private void button2_Click(object sender, EventArgs e)
{
Form1 ss = new Form1();
using (SqlCommand comd = new SqlCommand("select 编号,姓名,折扣 from 会员表 where 编号='" + id + "'", Sanck.con))
{
Sanck.con.Open();
SqlDataReader dr = comd.ExecuteReader();
if (dr.HasRows)
{
ss.fv = this;
dr.Read();
this.findv = new string[3] { dr[0].ToString(), dr[1].ToString(), dr[2].ToString() };
}
dr.Close();
Sanck.con.Close();
}
ss.ShowDialog();
this.Hide();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//获取到点击行所属的id
id = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
}
}
}