NullReferenceException使用两个构造函数类

时间:2015-12-24 01:09:27

标签: c# asp.net winforms nullreferenceexception

我是新来的。我无法在任何地方找到解决问题的方法。我正在写一个项目到学校。我有两种形式。在第一种形式中,当我将数据存储在SQL Server数据库中时,我有一个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");
            }
        }

两个listBoxes的值都不为null,我已经检查了这个。主要问题可能是类构造函数。我的存储过程工作正常,当我是从第一个表单传递数据的注释构造函数时,它完美地将数据传递给数据库。那么,怎么了?我应该对类的构造函数做什么,它会在两种情况下都有效?

我正在粘贴错误详情

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: 

1 个答案:

答案 0 :(得分:0)

我不确定,这段代码是否正确,但最后它开始起作用了!我有类似的解决方案,here但我不知道在我的主题问题中构造函数之间的差异是什么。在我看来,我的第一张唱片也应该完美。这是我的代码:

uniqvalue