简体   繁体   English

使用全局字符串变量时 C# 空引用错误

[英]C# null reference error when using a global string variable

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.我正在制作一个管理用户数据的程序,例如当第一个用户喜欢兰博基尼 Aventador 和第二个用户喜欢法拉利 F12b 时,程序会询问用户他们喜欢什么。 According to the program, first user enters Lamborghini Aventador and the second one enters F12b.根据程序,第一个用户输入兰博基尼 Aventador,第二个用户输入 F12b。 To make the program, I used a C# Windows form project in Microsoft Visual Studio 2013.为了制作程序,我在 Microsoft Visual Studio 2013 中使用了一个 C# Windows 窗体项目。

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.然后我做了一个button1和一个textbox1,当第一个用户在textbox1中输入他喜欢的东西(这是兰博基尼Aventador)时,他必须点击button1。 After clicking, the software is ready for the next user to type his favorite car which is Ferrari F12b.单击后,该软件已准备好让下一个用户输入他最喜欢的汽车,即法拉利 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.然后我做了一个button2和textbox2,当你在textbox2上输入“1”并点击button2时,它会在textbox2中显示第一个用户喜欢什么车(Aventador)。

Actually I created a class called "car" which has a string which is called "fav_car" AND a integer called "user_id".实际上,我创建了一个名为“car”的类,它有一个名为“fav_car”的字符串和一个名为“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:对于按钮 1:

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

for the button2:对于按钮 2:

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.if (textbox2.text==a[i].user_id)行中。
How to solve it and why that happened?如何解决它以及为什么会发生这种情况? My a[i] string was global, it shouldn't have happened!我的a[i]字符串是全局的,它不应该发生!

**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. ** **编辑: a[i] 是数组(不是字符串),我写错了,它是“For (int j=0; j<2 ;j++) 并且对于 button2 它是 a[j] 而不是 a[ i] 这些是我帖子中的错误,我真正的问题是“textbox2.text==a[i].user_id”行的空引用。**

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:您应该首先初始化您的项目,并且数组索引也是从零开始的,将j<=2更改为j<2或创建一个包含 3 个元素的数组:

car[] a = new car[3];

Before a[i].fav_car=textbox1.Text;之前a[i].fav_car=textbox1.Text; you need to initialize a[i] like: a[i] = new car();您需要像这样初始化a[i]a[i] = new car();

My a[i] string was global,我的 a[i] 字符串是全局的,

a[i] is not a string , you have an array or cars , so it is a car and it's null by default. a[i]不是string ,您有一个数组或cars ,因此它是car并且默认情况下为null

Use List instead of a array.使用 List 而不是数组。 It´s more useful and easier to work.它更有用,更容易工作。

http://msdn.microsoft.com/pt-br/library/6sh2ey19(v=vs.110).aspx 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;
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM