null exception using string arrays [duplicate]

  Uncategorised

This is the code I have now:

public List<string> carBrands;
public List<string> carModels;
public List<string> carQualities;

string input = "volvo 240 = good"
string[] s = input.Split(' ');

int i = 0;
int numberOfCars = 0;
while (i < s.Length - 1)
{
    if (s[i] == "=")
    {
        carBrands.Append(s[i - 2]);
        rtbStatus.Text += "Car Brand: " + carBrands[numberOfCars];
        carModels.Append(s[i - 1]);
        rtbStatus.Text += "Car Model: " + carModels[numberOfCars];
        carQualities.Append(s[i + 1]);
        rtbStatus.Text += "Car Quality: " + carQualities[numberOfCars];
        numberOfCars++;
    }
    else
    {
        i++;
    }
}

what i want this code to do is take the input string "volvo 240 = good", split it and all that good stuff, then the iterator (int i = 0), that counts up all the time, reaches the "=" I want it to load "volvo" into carBrands (since "=" is [i], that should be [i – 2]), "240" into carModels, and "good" into carQualities.

Also it says that that string is null, i do suspect it is a problem with the iterator counting backwards but i don’t think that should be a factor?

Just tell me if you need a more extensive description or any other kind of data.

Source: Visual Studio Questions

LEAVE A COMMENT