对象引用未设置为对象的实例,哪个对象是编译器引用的? [英] object reference not set to an instance of an object problem which object is the compiler referring?

查看:44
本文介绍了对象引用未设置为对象的实例,哪个对象是编译器引用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;使用System.Windows
;使用System.Windows.Controls
;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
使用System.Data;
使用System.Data.SqlClient;
使用System.IO;

名称空间hrms_hrmodule
{
///< summary>
/// Register.xaml的互动逻辑
///< / summary>
public partial class注册:Window
{
public SqlConnection con;
public SqlCommand cmd;
public byte [] doc1,doc2,pic1;
公共性别;
public Register()
{
InitializeComponent();

}
private void load_data(object sender,RoutedEventArgs e)
{


}

private void back(object sender,RoutedEventArgs e)
{
MainWindow main = new MainWindow();
this.Hide();
main.Show();
}
public void loadimage(object sender,RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.ShowDialog();
FileStream fs = new FileStream(dlg.FileName,FileMode.Open,FileAccess.Read);
Byte [] pic1 = new Byte [fs.Length];
fs.Read(pic1,0,Convert.ToInt32(fs.Length));
text_photo.Text = dlg.SafeFileName.ToString();


}
public void loaddoc1(object sender,RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog flg = new Microsoft.Win32.OpenFileDialog( );
flg.ShowDialog();
FileStream fd = new FileStream(flg.FileName,FileMode.Open,FileAccess.Read);
Byte [] doc1 = new Byte [fd.Length];
fd.Read(doc1,0,Convert.ToInt32(fd.Length));
text_doc1.Text = flg.SafeFileName.ToString();
}
public void loaddoc2(object sender,RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog fl = new Microsoft.Win32.OpenFileDialog();
fl.ShowDialog();
FileStream fg = new FileStream(fl.FileName,FileMode.Open,FileAccess.Read);

Byte [] doc2 = new Byte [fg.Length];
fg.Read(doc2,0,Convert.ToInt32(fg.Length));
text_doc2.Text = fl.SafeFileName.ToString();
}
public void savedata(object sender,RoutedEventArgs e)
{
con = new SqlConnection(@Data Source =(LocalDB)\ MSSQLLocalDB; AttachDbFilename = E:\\ \\ Software \Hrms \hrms_hrmodule \hrms_hrmodule \ employee.mdf; Integrated Security = True;);
尝试
{
con.Open();
string s =插入注册值(@ empid,@ name,@ age,@ dob,@ add,@ contact,@ sex,@ skill,@ prof,@ photo,@ doc1,@ doc2) ;

cmd.Parameters.Add(new SqlParameter(@ empid,SqlDbType.Int));
cmd.Parameters.Add(new SqlParameter(@ name,SqlDbType.Text));
cmd.Parameters.Add(new SqlParameter(@ age,SqlDbType.Int));
cmd.Parameters.Add(new SqlParameter(@ dob,SqlDbType.Date));
cmd.Parameters.Add(new SqlParameter(@ add,SqlDbType.Text));
cmd.Parameters.Add(new SqlParameter(@ contact,SqlDbType.Text));
cmd.Parameters.Add(new SqlParameter(@ sex,SqlDbType.Text));
cmd.Parameters.Add(new SqlParameter(@ skill,SqlDbType.Text));
cmd.Parameters.Add(new SqlParameter(@ prof,SqlDbType.Text));
cmd.Parameters.Add(new SqlParameter(@ photo,SqlDbType.Image));
cmd.Parameters.Add(new SqlParameter(@ doc1,SqlDbType.Image));
cmd.Parameters.Add(new SqlParameter(@ doc2,SqlDbType.Image));

cmd.Parameters [@ empid]。Value = text_id.Text;
cmd.Parameters [@ name]。Value = text_name.Text;
cmd.Parameters [@ age]。Value = text_age.Text;
cmd.Parameters [@ add]。Value = text_address.Text;
cmd.Parameters [@ contact]。Value = text_contact.Text;
cmd.Parameters [@ dob]。Value = datepicker.Text;
cmd.Parameters [@ prof]。Value = comboBox1.Text;
cmd.Parameters [@ skill]。Value = comboBox.Text;
cmd.Parameters [@ photo]。值= pic1;
cmd.Parameters [@ doc1]。Value = doc1;
cmd.Parameters [@ doc2]。Value = doc2;
cmd.Parameters [@ sex]。Value = combo_sex.Text;
cmd = new SqlCommand(s,con);
cmd.ExecuteNonQuery();
MessageBox.Show(record saved);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);

}
}



}
}

解决方案

您是否调试过代码?哪条线你得到这个错误..?当您尝试使用空引用执行操作时,会出现此类错误,调试代码并检查您收到此错误的行,然后检查实例是否包含值。

在不运行代码的情况下,我们无法确定 - 但如果您在调试器中运行它,它将停在导致问题的行上,这应该是显而易见的,特别是如果您使用调试器查看变量及其包含的内容。



但是......我猜它是你的cmd对象 - 我在该代码中看不到任何内容它给了它一个价值...



就个人而言,我不会将Connection或Command对象声明为类级别 - 将它们保持为本地并使用使用块确保它们正确处置:

 使用(SqlConnection con =  new  SqlConnection(strConnect))
{
con.Open();
使用(SqlCommand cmd = new SqlCommand( ...,con))
{
...

这也有优点是你不能使用它,除非它有一个值,因为它超出了使用块末尾的范围,如果你试图使用它,编译器会抱怨。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace hrms_hrmodule
{
    /// <summary>
    /// Interaction logic for Register.xaml
    /// </summary>
    public partial class Register : Window
    {
        public SqlConnection con;
        public SqlCommand cmd;
        public byte[] doc1, doc2, pic1;
        public string sex;
        public Register()
        {
            InitializeComponent();

        }
        private void load_data(object sender, RoutedEventArgs e)
        {


        }

        private void back(object sender, RoutedEventArgs e)
        {
            MainWindow main = new MainWindow();
            this.Hide();
            main.Show();
        }
        public void loadimage(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.ShowDialog();
            FileStream fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read);
            Byte[] pic1 = new Byte[fs.Length];
            fs.Read(pic1, 0, Convert.ToInt32(fs.Length));
            text_photo.Text = dlg.SafeFileName.ToString();


        }
        public void loaddoc1(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog flg = new Microsoft.Win32.OpenFileDialog();
            flg.ShowDialog();
            FileStream fd = new FileStream(flg.FileName, FileMode.Open, FileAccess.Read);
            Byte[] doc1 = new Byte[fd.Length];
            fd.Read(doc1, 0, Convert.ToInt32(fd.Length));       
            text_doc1.Text = flg.SafeFileName.ToString();
        }
        public void loaddoc2(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog fl = new Microsoft.Win32.OpenFileDialog();
            fl.ShowDialog();
            FileStream fg = new FileStream(fl.FileName, FileMode.Open, FileAccess.Read);
            
            Byte[] doc2 = new Byte[fg.Length];
            fg.Read(doc2, 0, Convert.ToInt32(fg.Length));
            text_doc2.Text = fl.SafeFileName.ToString();
        }
        public void savedata(object sender, RoutedEventArgs e)
        {
            con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\Software\Hrms\hrms_hrmodule\hrms_hrmodule\employee.mdf;Integrated Security=True;");
            try
            {
                con.Open();
                string s = "insert into Registration values(@empid,@name,@age,@dob,@add,@contact,@sex,@skill,@prof,@photo,@doc1,@doc2)";

                cmd.Parameters.Add(new SqlParameter("@empid",SqlDbType.Int));
                cmd.Parameters.Add(new SqlParameter("@name",SqlDbType.Text));
                cmd.Parameters.Add(new SqlParameter("@age", SqlDbType.Int));
                cmd.Parameters.Add(new SqlParameter("@dob", SqlDbType.Date));
                cmd.Parameters.Add(new SqlParameter("@add",SqlDbType.Text));
                cmd.Parameters.Add(new SqlParameter("@contact",SqlDbType.Text));
                cmd.Parameters.Add(new SqlParameter("@sex",SqlDbType.Text));
                cmd.Parameters.Add(new SqlParameter("@skill",SqlDbType.Text));
                cmd.Parameters.Add(new SqlParameter("@prof",SqlDbType.Text));
                cmd.Parameters.Add(new SqlParameter("@photo",SqlDbType.Image));
                cmd.Parameters.Add(new SqlParameter("@doc1",SqlDbType.Image));
                cmd.Parameters.Add(new SqlParameter("@doc2", SqlDbType.Image));

                cmd.Parameters["@empid"].Value = text_id.Text;
                cmd.Parameters["@name"].Value = text_name.Text;
                cmd.Parameters["@age"].Value = text_age.Text;
                cmd.Parameters["@add"].Value = text_address.Text;
                cmd.Parameters["@contact"].Value = text_contact.Text;
                cmd.Parameters["@dob"].Value = datepicker.Text;
                cmd.Parameters["@prof"].Value = comboBox1.Text;
                cmd.Parameters["@skill"].Value =comboBox.Text;
                cmd.Parameters["@photo"].Value = pic1;
                cmd.Parameters["@doc1"].Value = doc1;
                cmd.Parameters["@doc2"].Value = doc2;
                cmd.Parameters["@sex"].Value = combo_sex.Text;
                cmd = new SqlCommand(s,con);
                cmd.ExecuteNonQuery();
                MessageBox.Show("record saved");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
          }
             
    
 
    }
}

解决方案

Have you debug the code.? Which line you got this error..? Object reference error is getting when you are trying to do action using null reference at that time you go this type of error, debug the code and check which line you got this error and then check that whether instance contains value or not.


Without running your code, we can't be sure - but if you run it in the debugger it will stop on the line that causes the problem and it should be obvious, particularly if you use the debugger to look at the variables and what they contain.

But...I'd guess it's your "cmd" object - I don't see anything in that code which gives it a value at all...

Personally, I wouldn't declare the Connection or Command objects as class level - keep them local and use a using block to ensure they are Disposed properly:

using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand(" ... ", con))
        {
        ...

That also has the advantage that you can't use it unless it has a value, because it goes out of scope at the end of the using block and the compiler will complain if you try to use it.


这篇关于对象引用未设置为对象的实例,哪个对象是编译器引用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆