System.NullReferenceException:未将对象引用设置为对象的实例

时间:2012-12-02 03:47:36

标签: c#

我的aConnection.Open()行收到错误,无法找出原因。这个确切的代码工作,然后我尝试添加基本相同的代码到另一个类将值插入到Vehicle表中,我开始收到此错误。所以我删除了所有内容,当它工作时我已经将它删除了,单击“保存”时仍然出错。有任何想法吗?我已经耗尽了寻找和解决方案的所有资源。谢谢!

数据层

public class Data
{
    public static Business anApplicant;

    static SqlConnection aConnection = null;

    public static void initializeConnection(SqlConnection aDbConnection)
    {
        aConnection = aDbConnection;
        aConnection.Open();
    }

    // Method for Inserting Applicant information into the database
    public static void InsertApplicant()
    {
        try
        {
            SqlCommand myCommand = new SqlCommand("INSERT INTO Applicant (FirstName, LastName, Address, City, State, Zip, Phone, Gender, BirthDate)" +
                "VALUES ('" + anApplicant.FName + "', '" + anApplicant.LName + "', '" + anApplicant.Address + "', '" + anApplicant.City + "', '" + anApplicant.State +
                "', '" + anApplicant.Zip + "', '" + anApplicant.Phone + "', '" + anApplicant.Gender + "', '" + anApplicant.BirthDate + "')", aConnection);

            if (aConnection.State == ConnectionState.Closed)
                aConnection.Open();

            myCommand.ExecuteNonQuery();

            aConnection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Error");
        }
    }
}

业务层

public class Business
{
    SqlConnection aConnection = new SqlConnection("Data Source=zac2424-HP; Initial Catalog=Final; Trusted_Connection=True;");

    public void initializeConnection() 
    { 
        Data.initializeConnection(aConnection); 
    }

    private string policyNumber;
    private string fName;
    private string lName;
    private string address;
    private string city;
    private string state;
    private string zip;
    private string phone;
    private string gender;
    private string birthDate;

    public Business(string fName, string lName, string address,
        string city, string state, string zip, string phone, string gender, string birthDate)
    {
        FName = fName;
        LName = lName;
        Address = address;
        City = city;
        State = state;
        Zip = zip;
        Phone = phone;
        Gender = gender;
        BirthDate = birthDate;
    }

    public Business()
    {
    }

    // Applicant Get and Set Method
    public string PolicyNumber
    {
        get { return policyNumber; }
        set { policyNumber = value; }
    }

    public string FName
    {
        get { return fName; }
        set { fName = value; }
    }

    public string LName
    {
        get { return lName; }
        set { lName = value; }
    }

    public string Address
    {
        get { return address; }
        set { address = value; }
    }

    public string City
    {
        get { return city; }
        set { city = value; }
    }

    public string State
    {
        get { return state; }
        set { state = value; }
    }

    public string Zip
    {
        get { return zip; }
        set { zip = value; }
    }

    public string Phone
    {
        get { return phone; }
        set { phone = value; }
    }

    public string Gender
    {
        get { return gender; }
        set { gender = value; }
    }

    public string BirthDate
    {
        get { return birthDate; }
        set { birthDate = value; }
    }

    string premium = "";
    public string Premium
    {
        get { return premium; }
        set { premium = value; }
    }
}

表示层

public partial class PolicyHomeForm : Form
{
    public PolicyHomeForm()
    {
        InitializeComponent();
    }

    private void PolicyHomeForm_Load(object sender, EventArgs e)
    {

    }

    public void saveButton_Click(object sender, EventArgs e)
    {
        Data.anApplicant = new Business(txtFirstName.Text, txtLastName.Text, txtAddress.Text, txtCity.Text, comboState.Text, txtZip.Text, txtPhone.Text,
            comboGender.Text, txtBirthDate.Text);

        //Data.aVehicle = new Vehicle(comboMake.Text, txtModel.Text, txtYear.Text, txtDesc.Text);

        Data.InsertApplicant();

        //Data.InsertVehicle();
    }
}

2 个答案:

答案 0 :(得分:2)

您的代码从不调用Data.initializeConnection,因此Data.aConnection始终为空。

答案 1 :(得分:0)

空引用异常是您尝试访问空对象或未初始化对象的错误。 (在这种情况下,您传递的参数SQLConnection为null)。

其中一个解决方案是在构造函数中初始化SQLconnection对象并将连接字符串作为参数传递,而不是SQLConnection

或者另一种解决方案是将连接放在数据访问层中,在这种情况下,在Data类中创建一个构造函数并在那里打开连接。

抱歉无济于事,因为我是通过手机发布的^^ ...希望有所帮助