Srihari
Srihari

Reputation: 2429

Object reference not set to an instance of an object error in C# winform devexpress?

I have Gridview in my Form, If i click button on the Gridview I get Column value of Focused Row and Try to use that Value in next Form. But in that new form error shown like this

 public partial class New_Invoice : DevExpress.XtraEditors.XtraForm
{

    string getOper = "A";

    public New_Invoice()
    {
        InitializeComponent();
    }

    public New_Invoice(string oper, int invoiceno)
    {
        // TODO: Complete member initialization

        textEdit5.Text = invoiceno.ToString(); // error shown in this line
        textEdit5.Visible = false;
        getOper = oper;
    }  

What was wrong in my code ?

Upvotes: 1

Views: 1702

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064244

In your custom constructor you aren't calling InitializeComponent(). This is critical: this is what creates the controls. A simple fix might be to chain the constructor (see the : this()):

public New_Invoice(string oper, int invoiceno) : this()
{
    textEdit5.Text = invoiceno.ToString(); // error shown in this line
    textEdit5.Visible = false;
    getOper = oper;
} 

However, personally I would advise against adding custom constructors to forms / controls, and instead use properties / methods on the newly created instance, so the caller does something like:

using(var form = new NewInvoice())
{
    form.Operation = ...;     // (or maybe form.SetInvoiceNumber(...) if the
    form.InvoiceNumber = ...; // logic is non-trivial)
    // show the form, etc
}

Upvotes: 5

Related Questions