Using System. Collections. Generic;
Using System.Com ponentModel;
Using System. The Data;
Using System. Drawing;
Using System. Linq;
Using System. The Text;
Using System. The Threading. The Tasks;
Using System. Windows. Forms;
The namespace WindowsFormsApp1
{
Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent ();
}
The DataTable DTT;
The DataTable dt.
Private void button1_Click (object sender, EventArgs e)
{
The DataTable dt=new DataTable ();
DataColumn comName=new DataColumn (" brand name ", typeof (string));
DataColumn comPrice=new DataColumn (" price ", typeof (int));
DataColumn comInventory=new DataColumn (" inventory ", the typeof (int));
Dt. Columns. The Add (" brand name ", typeof (string));
Dt. Columns. The Add (" price ", typeof (int));
Dt. Columns. The Add (" inventory ", typeof (int));
Dt. Rows. The Add (" pencil ", 1, 200);
Dt. Rows. The Add (" folder ", 6, 50);
DataGridView1. The DataSource=dt;
}
Private void button2_Click (object sender, EventArgs e)
{
The DataTable DTT=new DataTable ();
DataColumn comName=new DataColumn (" brand name ", typeof (string));
DataColumn comPrice=new DataColumn (" price ", typeof (int));
DataColumn comInventory=new DataColumn (" inventory ", the typeof (int));
DTT. Columns. The Add (" brand name ", typeof (string));
DTT. Columns. The Add (" price ", typeof (int));
DTT. Columns. The Add (" inventory ", typeof (int));
DTT. Rows. The Add (" pencil ", 3, 600);
DTT. Rows. The Add (" folder ", 5, 800);
DataGridView2. The DataSource=DTT;
}
Private void button3_Click (object sender, EventArgs e)
{
Dt. Merge (DTT);//the problem here
DataGridView3. The DataSource=dt;
}
}
}
The above code compiler can pass, the button1_Click with button2_Click code works, abnormal, when button3_Click hint information is as follows:
* * * * * * * * * * * * * * exception text * * * * * * * * * * * * * *
System. NullReferenceException: the object reference not set to an instance of an object,
In WindowsFormsApp1. Form1. Button3_Click (Object sender, EventArgs e) location D: \ \ WindowsFormsApp1 data table operation \ WindowsFormsApp1 \ Form1 cs: line number 62
In the System. Windows. Forms. Control. The OnClick (EventArgs e)
In the System. Windows. Forms. The Button. The OnClick (EventArgs e)
In the System. Windows. Forms. Button. onm ouseUp (MouseEventArgs mevent)
In the System. Windows. Forms. Control. WmMouseUp (Message& M, MouseButtons button, Int32 on)
In the System. Windows. Forms. Control. WndProc (Message& M)
In the System. Windows. Forms. ButtonBase. WndProc (Message& M)
In the System. Windows. Forms. Button. WndProc (Message& M)
In the System. Windows. Forms. Control. ControlNativeWindow. OnMessage (Message& M)
In the System. Windows. Forms. Control. ControlNativeWindow. WndProc (Message& M)
In the System. Windows. Forms. NativeWindow. Callback (IntPtr hWnd, Int32 MSG, IntPtr wparam, IntPtr lparam)
Please comment
CodePudding user response:
The most basic written procedure is to adjust, in the wrong place will interrupt, you first see the exception typeSystem. NullReferenceException: the object reference not set to an instance of an object,
If you don't understand just read a few times more, Chinese writing is clear, if still don't understand, take exception information to search engine search, look at the others how to solve,
The null reference here you
You see if dt or DTT null
Learn to debug key, to the company no colleagues will help you debug, not even when you finish running, out of the bug, others help you debugging good, leadership will only remember you made a mistake, another colleague severe solves a problem than you,