开发者

Object reference not set to an instance of an object... Error while using foreach [duplicate]

开发者 https://www.devze.com 2023-03-23 06:36 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate:开发者_开发问答 What is a NullReferenceException in .NET?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:开发者_开发问答

What is a NullReferenceException in .NET?

I am developing a simple program. Accepting an arraylist from Form1 and displaying its contents on form2 and I am getting this error.. Plz enlighten me.. Coding goes like this ..

FORM1.cs..............

public partial class Form1 : Form
{
    ArrayList alcar;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string name = textBox1.Text; ; int quantity = int.Parse(textBox2.Text);
        car c = new car(name, quantity);
        if (alcar != null)
        {
            alcar.Add(c);
        }
        else
        {
            alcar = new ArrayList();
            alcar.Add(c);
        }
        Form2 f2 = new Form2();
        f2.Show();
    }
    public ArrayList getArray()
    {
     return alcar;
    }    
}
class car
{
    string name; int quantity;
    public string NAME
    {
        get { return name; }
        set { name = value; }
    }
    public int QUANTITY
    {
        get { return quantity; }
        set { quantity = value; }
    }
    public car(string n, int q)
    {
        name = n; quantity = q;
    }
}

FORM2.cs.........................

public partial class Form2 : Form
{
    ArrayList al;
    public Form2()
    {
        Form1 f1 = new Form1();
        if (al != null)
        {
            al = f1.getArray();
        }
        else
        {
            al = new ArrayList();
            al = f1.getArray();
        }

        InitializeComponent();
        foreach (car c in al)        // this lne is causing error
        {
            label1.Text = c.NAME;
            label2.Text = Convert.ToString(c.QUANTITY);
        }
    }

}


If f1.getArray() returns a null, that's the Exception I would expect.

But the issue is here:

// In Form2
Form1 f1 = new Form1();

This creates a new Form1 instance, not the instance that you created the car in. It will have nothing in the array. In this new instance, you never initialize alcar or populate it, so it is null by default. Getting this null object and trying to iterate over it is where your error comes from.

Rethink your design so you pass the array to the second form directly (either on a custom constructor or a property).


Why don't you write new in the field declaration itself, which will simplify your code a lot:

public partial class Form1 : Form
{

    ArrayList alcar = new ArrayList(); // DO THIS HERE!

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string name = textBox1.Text; ; int quantity = int.Parse(textBox2.Text);
        car c = new car(name, quantity);

        //JUST ADD - without checking for null!
        alcar.Add(c);

        Form2 f2 = new Form2();
        f2.Show();
    }
    public ArrayList getArray()
    {
         return alcar; //its non-null - guaranteed!
    }
    //...
 }


Your creating a new instance of Form1 in the ctor of Form2, it never shows it's UI, and never gets clicked therefore when you say al=f1.getArray(); in the ctor of Form2 it returns a null reference.

Use the reference of Form1 that exists already instead of creating a new one.


ArrayList alcar isn't instantiated in Form1 unless the button is clicked. You need to check for null and instantiate alcar in the GetArray method of Form1. Or, instantiate alcar in Form1's constructor.


In Form2, you create a new instance of Form1 : Form1 f1 = new Form1();

You never initiated alcar for that particular instance. You need to get the instance of Form1 where the user have clicked button1.


Try this

public ArrayList getArray()
{
    if(null == alcar)
         alcar = new ArrayList();

    return alcar;
}


ArrayList alcar; created without element and when you call the GetArray() will return it null and you have your exception one of the sollution you can make in Form1

        Form2 f2 = new Form2(alcar);
        f2.Show();

and in form2

     ArrayList al = new ArrayList();
     public Form2(ArrayList  alcar)
     {
         al = alcar;
         InitializeComponent();
0

精彩评论

暂无评论...
验证码 换一张
取 消