NullReferenceException 使用两个构造函数 class
NullReferenceException using two constructor class
我是新来的。我无法在任何地方找到解决问题的方法。我正在给学校写一个项目。我有两种形式。在第一种形式中,当我将数据存储在 SQL 服务器数据库中时,我有一个 DataGridView 对象。我使用代码将用户名从 DataGridView 传递到第二种形式:
List<string> usersList = new List<string>();
for (int i = 0; i < dataGridViewUsers.RowCount; i++)
{
usersList.Add(dataGridViewUsers.Rows[i].Cells[1].Value.ToString());
}
AddFileForm addFile = new AddFileForm(usersList);
addFile.Show();
在第二种形式中有以下代码:
private List<string> userNames;
private Files file;
public AddFileForm()
{
file = new Files();
userNames = new List<string>();
InitializeComponent();
}
public AddFileForm(List<string> _userNames)
{
this.userNames = _userNames;
InitializeComponent();
listBoxUserList.DataSource = _userNames;
}
private AddFileForm(Files _file)
{
this.file = _file;
InitializeComponent();
listBoxUserList.SelectedValue = _file.userName;
listBoxItems.SelectedValue = _file.directory;
}
从第一个表单传递数据工作正常,但是当我尝试使用存储过程将数据从 listBoxUserList 和 listBoxItems 传递到数据库时,问题就出现了。我正在手动将数据添加到 listBoxItems。单击按钮时发生 NullReferenceException,此处:
private void buttonAdd_Click(object sender, EventArgs e)
{
if (listBoxUserList.SelectedItems.Count != 0 && listBoxItems.SelectedItems.Count != 0)
{
file.userName = listBoxUserList.SelectedValue.ToString();
file.directory = listBoxItems.SelectedValue.ToString();
try
{
DBConnection connection = new DBConnection();
connection.AddFile(file);
MessageBox.Show("File added to database.");
}
catch (Exception)
{
MessageBox.Show("Failed database connection!");
}
}
else
{
MessageBox.Show("First, select items to add to database");
}
}
两个列表框的值都不为空,我已经检查过了。主要问题可能出在 class 构造函数中。我的存储过程工作正常,当我是从第一种形式传递数据的注释构造函数时,它完美地将数据传递到数据库。那么,怎么了?我应该如何处理 class 的构造函数,它在两种情况下都可以 属性 工作?
我正在粘贴错误详细信息
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Odwołanie do obiektu nie zostało ustawione na wystąpienie obiektu.
Source=FilesEncoding
StackTrace:
w FilesEncoding.AddFileForm.buttonAdd_Click(Object sender, EventArgs e) w C:\Users\Grabarz\Documents\Visual Studio 2010\Projects\FilesEncoding\FilesEncoding\AddFileForm.cs:wiersz 110
w System.Windows.Forms.Control.OnClick(EventArgs e)
w System.Windows.Forms.Button.OnClick(EventArgs e)
w System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
w System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
w System.Windows.Forms.Control.WndProc(Message& m)
w System.Windows.Forms.ButtonBase.WndProc(Message& m)
w System.Windows.Forms.Button.WndProc(Message& m)
w System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
w System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
w System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
w System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
w System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
w System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
w System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
w System.Windows.Forms.Application.Run(Form mainForm)
w FilesEncoding.Program.Main() w c:\users\grabarz\documents\visual studio 2010\Projects\FilesEncoding\FilesEncoding\Program.cs:wiersz 18
w System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
w System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
w Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
w System.Threading.ThreadHelper.ThreadStart_Context(Object state)
w System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
w System.Threading.ThreadHelper.ThreadStart()
InnerException:
我不确定这段代码是否正确,但它终于开始工作了!我有类似的解决方案,here 但我不知道我的主题问题中的构造函数和这个有什么不同。在我看来,我的第一张唱片也应该完美无缺。这是我的代码:
private string userName;
private string fileDirectory;
public AddFileForm(Files _file) : this(_file.userName, _file.directory) { }
public AddFileForm(string user, string directory)
{
userName = user;
fileDirectory = directory;
InitializeComponent();
}
private void buttonAdd_Click(object sender, EventArgs e)
{
userName = listBoxUserList.SelectedItem.ToString();
fileDirectory = listBoxItems.SelectedItem.ToString();
try
{
Files file = new Files();
file.userName = userName;
file.directory = fileDirectory;
DBConnection connection = new DBConnection();
connection.AddFile(file);
}
catch (Exception)
{
MessageBox.Show("Failed database connection!");
}
}
我是新来的。我无法在任何地方找到解决问题的方法。我正在给学校写一个项目。我有两种形式。在第一种形式中,当我将数据存储在 SQL 服务器数据库中时,我有一个 DataGridView 对象。我使用代码将用户名从 DataGridView 传递到第二种形式:
List<string> usersList = new List<string>();
for (int i = 0; i < dataGridViewUsers.RowCount; i++)
{
usersList.Add(dataGridViewUsers.Rows[i].Cells[1].Value.ToString());
}
AddFileForm addFile = new AddFileForm(usersList);
addFile.Show();
在第二种形式中有以下代码:
private List<string> userNames;
private Files file;
public AddFileForm()
{
file = new Files();
userNames = new List<string>();
InitializeComponent();
}
public AddFileForm(List<string> _userNames)
{
this.userNames = _userNames;
InitializeComponent();
listBoxUserList.DataSource = _userNames;
}
private AddFileForm(Files _file)
{
this.file = _file;
InitializeComponent();
listBoxUserList.SelectedValue = _file.userName;
listBoxItems.SelectedValue = _file.directory;
}
从第一个表单传递数据工作正常,但是当我尝试使用存储过程将数据从 listBoxUserList 和 listBoxItems 传递到数据库时,问题就出现了。我正在手动将数据添加到 listBoxItems。单击按钮时发生 NullReferenceException,此处:
private void buttonAdd_Click(object sender, EventArgs e)
{
if (listBoxUserList.SelectedItems.Count != 0 && listBoxItems.SelectedItems.Count != 0)
{
file.userName = listBoxUserList.SelectedValue.ToString();
file.directory = listBoxItems.SelectedValue.ToString();
try
{
DBConnection connection = new DBConnection();
connection.AddFile(file);
MessageBox.Show("File added to database.");
}
catch (Exception)
{
MessageBox.Show("Failed database connection!");
}
}
else
{
MessageBox.Show("First, select items to add to database");
}
}
两个列表框的值都不为空,我已经检查过了。主要问题可能出在 class 构造函数中。我的存储过程工作正常,当我是从第一种形式传递数据的注释构造函数时,它完美地将数据传递到数据库。那么,怎么了?我应该如何处理 class 的构造函数,它在两种情况下都可以 属性 工作?
我正在粘贴错误详细信息
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Odwołanie do obiektu nie zostało ustawione na wystąpienie obiektu.
Source=FilesEncoding
StackTrace:
w FilesEncoding.AddFileForm.buttonAdd_Click(Object sender, EventArgs e) w C:\Users\Grabarz\Documents\Visual Studio 2010\Projects\FilesEncoding\FilesEncoding\AddFileForm.cs:wiersz 110
w System.Windows.Forms.Control.OnClick(EventArgs e)
w System.Windows.Forms.Button.OnClick(EventArgs e)
w System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
w System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
w System.Windows.Forms.Control.WndProc(Message& m)
w System.Windows.Forms.ButtonBase.WndProc(Message& m)
w System.Windows.Forms.Button.WndProc(Message& m)
w System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
w System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
w System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
w System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
w System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
w System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
w System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
w System.Windows.Forms.Application.Run(Form mainForm)
w FilesEncoding.Program.Main() w c:\users\grabarz\documents\visual studio 2010\Projects\FilesEncoding\FilesEncoding\Program.cs:wiersz 18
w System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
w System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
w Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
w System.Threading.ThreadHelper.ThreadStart_Context(Object state)
w System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
w System.Threading.ThreadHelper.ThreadStart()
InnerException:
我不确定这段代码是否正确,但它终于开始工作了!我有类似的解决方案,here 但我不知道我的主题问题中的构造函数和这个有什么不同。在我看来,我的第一张唱片也应该完美无缺。这是我的代码:
private string userName;
private string fileDirectory;
public AddFileForm(Files _file) : this(_file.userName, _file.directory) { }
public AddFileForm(string user, string directory)
{
userName = user;
fileDirectory = directory;
InitializeComponent();
}
private void buttonAdd_Click(object sender, EventArgs e)
{
userName = listBoxUserList.SelectedItem.ToString();
fileDirectory = listBoxItems.SelectedItem.ToString();
try
{
Files file = new Files();
file.userName = userName;
file.directory = fileDirectory;
DBConnection connection = new DBConnection();
connection.AddFile(file);
}
catch (Exception)
{
MessageBox.Show("Failed database connection!");
}
}