0

I'm trying to populate my first column with the values of the string array. I'm using the code below. Which is not working as I'm intended to use it. It throws "Object reference not set to an instance of an object". Could someone explain what I'm doing wrong and suggest a correct approach?

private static int i = 0;
public static string[] names = new string[] { "cmd", "EUI", "ts", "fcnt", "port", "freq", "dr", "ack", "gws", "data" };
public static string[,] jdata;

static void Main(string[] args) {

        while (i++ < names.Length) {
            jdata[i,0] = names[i];
        }
}
Rapsoulis
  • 212
  • 3
  • 13

4 Answers4

2

You need to initialize your jdata array first. As the parameters you can use the length of the names array and the number of columns you want.

Also if your i variable is 0 at the beginning, you should increment it after you added a name to the new array or you will get a index out of range exception.

private static int i = 0;
public static string[] names = new string[] { "cmd", "EUI", "ts", "fcnt", "port", "freq", "dr", "ack", "gws", "data" };
public static string[,] jdata;

static void Main(string[] args) {
    jdata = new string[names.Length, 1];
    while (i < names.Length) {
        jdata[i,0] = names[i];
        i++; 
    }
}
Bojan B
  • 1,882
  • 4
  • 16
  • 24
  • I do increment it. 'while (i++ < names.Length)' – Rapsoulis Feb 23 '17 at 12:43
  • @Rapsoulhs, yes you do however when you add the first element with your approach in this line: `jdata[i,0] = names[i];` the value of i is 1 not 0, so you skip the first element – Bojan B Feb 23 '17 at 12:48
2

Try using Lists.

    private static int i = 0;
    public static string[] names = new string[] { "cmd", "EUI", "ts", "fcnt", "port", "freq", "dr", "ack", "gws", "data" };
    public static List<string>[] jdata = new List<string>[names.Length];

    static void Main(string[] args)
    {
        for (int j = 0; j < names.Length; j++) jdata[j] = new List<string>();
        while (i < names.Length)
        {
            jdata[i].Add(names[i]);
            i++;
        }

        Console.ReadKey();
    }
1

to be able to use an object in a method it has to be instantiated first! Your array jdata has the value null so you cannot access it.

Initialize it first and give it the proper dimensions, so that it is clear how much memory has to be acquired in advance:

public static string[,] jdata = new string[names.Length, 1];

Also if you want to use your while-loop as it is you need to start with i at -1. Otherwise you will skip the first entry. And you should only run until names.Length-1

while (i++ < names.Length-1)
{
    jdata[i, 0] = names[i];
}

Why not using a classic for-loop? It does not byte:

for (int i = 0; i < names.Length; i++)
{
    jdata[i, 0] = names[i];
}

Am I able to change the size later? Meaning: changing value 1 to another size?

If you want to do that I would suggest to use a List. The Add method allows you to extend the size of the List. In this example it is a List of Lists, which you can imagine as a table with columns and rows. Only the not all columns have necessarily the same amount of rows.

static void Main(string[] args)
{

    List<string> names = new List<string> { "cmd", "EUI", "ts", "fcnt", "port", "freq", "dr", "ack", "gws", "data" };
    List<List<string>> jdata = new List<System.Collections.Generic.List<string>>

    jdata.Add(names);

    Console.ReadKey();
}

This has 2 Advantages:

1) The lists that you save in your columns can have different lengths

2) you can remove and add values as you please

To access the first column you can just use the [ ] operator:

List<string> savednames = jdata[0];
Mong Zhu
  • 20,890
  • 7
  • 33
  • 66
0

jdata is null..

    private static int i = 0;
    public static string[] names = new string[] { "cmd", "EUI", "ts", "fcnt", "port", "freq", "dr", "ack", "gws", "data" };
    public static string[,] jdata = new string[names.Length, 1];

    static void Main(string[] args)
    {

        while (i++ < names.Length)
        {
            jdata[i, 0] = names[i];
        }
    }
levent
  • 2,906
  • 1
  • 9
  • 17
  • In this case my array has a size of [10,1] which I don't want. I want [10,x] with x being populated later on. – Rapsoulis Feb 23 '17 at 12:39
  • 1
    @rapsoulhs: then don't use a multidimensional array: that size needs to be known beforehand. You can use an array of lists. Lists may grow. – JHBonarius Feb 23 '17 at 12:52