Pregunta

I'm making a program that manages users data, for example when first user likes Lamborghini Aventador and second user likes Ferrari F12b, the program asks user what they like. According to the program, first user enters Lamborghini Aventador and the second one enters F12b. To make the program, I used a C# Windows form project in Microsoft Visual Studio 2013.

Then I made a button1 and a textbox1, when the first user types in the textbox1 what he likes (which is Lamborghini Aventador) he must click on the button1. After clicking, the software is ready for the next user to type his favorite car which is Ferrari F12b.

Then I made a button2 and textbox2, when you type "1" on the textbox2 and click the button2, it shows you what car the first user likes (Aventador) int the textbox2.

Actually I created a class called "car" which has a string which is called "fav_car" AND a integer called "user_id". when user types his favorite car he actually types in the string of the class.

then I wrote a code like this:

global variables:

car []a=new car [2]; 
int i=0;

for the button1:

a[i].fav_car=textbox1.Text;
a[i].user_id=i;
i++;

for the button2:

for (int j=0; j<=2; j++) {
    if (textbox2.text==a[i].user_id) {
        textbox2.text==a[i].fav_car;
    }
}

But when compiling the code, it says

Null Reference error

in the if (textbox2.text==a[i].user_id) line.
How to solve it and why that happened? My a[i] string was global, it shouldn't have happened!

**EDIT: a[i] is array (not string), I made mistake in writing and it was "For (int j=0; j<2 ;j++) and also for button2 it was a[j] not a[i] those were mistakes in my post, My real problem was the null reference of the "textbox2.text==a[i].user_id" line. **

¿Fue útil?

Solución

You should initialize your items first and also array indexes are zero-based, either change j<=2 to j<2 or create an array with 3 elements:

car[] a = new car[3];

Before a[i].fav_car=textbox1.Text; you need to initialize a[i] like: a[i] = new car();

My a[i] string was global,

a[i] is not a string, you have an array or cars, so it is a car and it's null by default.

Otros consejos

Use List instead of a array. It´s more useful and easier to work.

http://msdn.microsoft.com/pt-br/library/6sh2ey19(v=vs.110).aspx

 List<car> _lstCar = new List<car>();

 car c = new car();

 c.fav_car=textbox1.Text;
 c.user_id= _lstCar.Count+1;

 _lstCar.Add(c);

foreach(Car c in _lstCar)
{
    //do some wor here
}

Try this. This do what you want.

List<car> globalList = new List<car>();
    int globalCounter = 0;
    void Button1_Click(object sender, RoutedEventArgs e)
    {
        car c = new car();

        c.fav_car = textbox1.Text;
        c.user_id = globalCounter++;

        globalList.Add(c);
    }

    void Button2_Click(object sender, RoutedEventArgs e)
    {
        foreach (car c in globalList)
        {
            if (textbox2.text == c.user_id)
            {
                textbox2.text == c.fav_car;
            }
        }
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top