簡體   English   中英

System.NullReferenceException:未將對象引用設置為對象的實例

[英]System.NullReferenceException: Object reference not set to an instance of an object

我在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();
    }
}

您的代碼從不調用Data.initializeConnection因此Data.aConnection始終為null。

空引用異常是您嘗試訪問空對象或未初始化對象的錯誤。 (在這種情況下,您傳遞的參數SQLConnection為null)。

其中一個解決方案是在構造函數中初始化SQLconnection對象並將連接字符串作為參數傳遞,而不是SQLConnection

或者另一種解決方案是將連接放在數據訪問層中,在這種情況下,在Data類中創建一個構造函數並在那里打開連接。

抱歉無能為力,因為我是從手機上發布的^^ ...希望有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM