Sreenath Ganga
Sreenath Ganga

Reputation: 736

Object reference not set to an instance of an object. exception at string assignment

This is my code to generate an autogenerating jobcode and fill it in a text box

          public void newjobecodenoget(){
         String jobcodeno = null;
        OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
        oleDbConnection1.Open();


        String query = "SELECT TOP 1 jobpk FROM jobcodemastertable ORDER BY jobpk DESC";
        OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);


        DataSet ds = new DataSet();

        dAdapter.Fill(ds, "tbljobrdataview");
        Int32  S = int.Parse(ds.Tables[0].Rows[0][0].ToString());
        S++;

         jobcodeno = "NFT" + S.ToString();
         MessageBox.Show( jobcodeno);
         txtjobcode.Text = "NFT" + S.ToString();

    }

but Iam getting exception at the last line ie txtjobcode.Text = "NFT" + S.ToString(); Object reference not set to an instance of an object. all other things are fine and jobcode is generated and shown in message box can anyone help pls

my txtjobecode was declared as

private void InitializeComponent()
        {
         this.txtjobcode = new System.Windows.Forms.TextBox();  
// 
            // txtjobcode
            // 
            this.txtjobcode.Location = new System.Drawing.Point(98, 23);
            this.txtjobcode.Name = "txtjobcode";
            this.txtjobcode.Size = new System.Drawing.Size(91, 20);
            this.txtjobcode.TabIndex = 0;
            //

} 

Upvotes: 0

Views: 290

Answers (2)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112324

Obviously, txtjobcode is null. Since I do not see where txtjobcode is declared and assigned to, I cannot tell you why it is null.

Upvotes: 2

BrokenGlass
BrokenGlass

Reputation: 160862

Since clearly S is declared and has a value it follows that txtjobcode is null, hence you get this (fairly self-explanatory) exception. You have not shown where you declare or use txtjobcode otherwise, but this is your problem.

Upvotes: 3

Related Questions