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];
        }
}
5
  • You need to initalize jdata before you try to use it Commented Feb 23, 2017 at 12:34
  • How do I initialize if I don't know the final size? Commented Feb 23, 2017 at 12:35
  • static string[,] jdata = new string[,] {}; Commented Feb 23, 2017 at 12:35
  • 2
    Possible duplicate of What is a NullReferenceException, and how do I fix it? Commented Feb 23, 2017 at 12:36
  • 1
    @Rapsoulhs in that case i think you need to use a list Commented Feb 23, 2017 at 12:36

4 Answers 4

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();
    }
Sign up to request clarification or add additional context in comments.

Comments

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++; 
    }
}

2 Comments

I do increment it. 'while (i++ < names.Length)'
@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
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];

5 Comments

Am I able to change the size later? Meaning: changing value 1 to another size?
unfortunately not. If you need to do that I would recommend to use List<T>s. They are flexible in size
@Rapsoulhs you could always wrap your array in an object, that handles these things for you
How do you do that?
@Rapsoulhs I made an edit and posted a version with Lists which allow for a variable size. Have a look
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];
        }
    }

2 Comments

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.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.