# Problem with string array?

**URL:** <https://forum.codewithmosh.com/t/problem-with-string-array/21261>\
**Category:** C#\
**Created:** [July 4, 2023, 4:39pm UTC](https://forum.codewithmosh.com/t/problem-with-string-array/21261 "2023-07-04T16:39:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ken76](https://avatars.discourse-cdn.com/v4/letter/k/ac91a4/32.png) [@Ken76](https://forum.codewithmosh.com/u/Ken76)\
**Post date:** [July 4, 2023, 4:39pm UTC](https://forum.codewithmosh.com/t/problem-with-string-array/21261/1 "2023-07-04T16:39:57Z")

</div>

I have a problem with this code, because it gives me following error message  
Object reference not set to an instance of an object when I use the line  
values[countValues] = reader2.GetString(“textinfo”); in the code below

Can someone help me?

```auto
public static string[] values;
 int countValues = 0;
 using (var connection = new MySqlConnection(connString))
                {
                    connection.Open();
                    string query = "select * from showvalues where idvalue = '" + idStatus + "'";
                    using (var command2 = new MySqlCommand(query, connection))
                    {
                        using (var reader2 = command2.ExecuteReader())
                        {
                            while (reader2.Read())
                            {
                          
                               listBoxMeasurement.Items.Add(reader2.GetString("textinfo"));
                               values[countValues] = reader2.GetString("textinfo");
                               countValues++;
                            }
                           countValues = 0;
                           connection.Close();
                         } 
                    }
                }

```

---

<div class="post-metadata">

**Author:** ![UniqueNospaceShort](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uniquenospaceshort/32/1814_2.png) [@UniqueNospaceShort](https://forum.codewithmosh.com/u/UniqueNospaceShort)\
**Post date:** [July 4, 2023, 5:55pm UTC](https://forum.codewithmosh.com/t/problem-with-string-array/21261/2 "2023-07-04T17:55:49Z")

</div>

Hi

Quick answer you are using an uninitialized array of string.  
You need to resize it to accept any new value.

[Check Array.Resize()](https://learn.microsoft.com/en-us/dotnet/api/system.array.resize?view=net-6.0)

Likely the reason why you get this error.

Can you share the structure of your table?

Cheers.

**EDIT**

Works perfectly

> DB Structure and data

![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/b/bd7786ee0a3e5e678d0c99563754a9f28c858df0.png)

> Main

```auto
var connectionString = MySqlCustomInfos.ConnectionString;

System.Console.WriteLine("\n\n=====\n1\n=====\n");
Ken76MySqlProblem.Method(connectionString, 1);
System.Console.WriteLine("\n\n=====\n2\n=====\n");
Ken76MySqlProblem.Method(connectionString, 2);

```

> 

```auto
using MySql.Data.MySqlClient;

class Ken76MySqlProblem
{
    public static void Method(string connString, int idStatus)
    {
        string[] values = new string[0];
        int countValues = 0;

        using (var connection = new MySqlConnection(connString))
        {
            connection.Open();
            string query = "SELECT * FROM showvalues WHERE idstatus = '" + idStatus + "'";

            using (var command2 = new MySqlCommand(query, connection))
            {
                using (var reader2 = command2.ExecuteReader())
                {
                    while (reader2.Read())
                    {
                        string result = reader2.GetString("textinfo");
                        // listBoxMeasurement.Items.Add(reader2.GetString("textinfo"));
                        Array.Resize(ref values, countValues + 1);
                        values[countValues] = reader2.GetString("textinfo");
                        countValues++;
                        System.Console.WriteLine($"{countValues} => {result}");
                    }
                    countValues = 0;
                    connection.Close();
                }
            }
        }

        System.Console.WriteLine($"\n\n=====\nIterating Array\n=====\n");
        Array.ForEach(values, (item) =>
        {
            System.Console.WriteLine($"Item => {item}");
        });
    }
}

```

> Output

 ![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/b/bef5a7419bd620f23cf25bae14b6fe530fed9d63.png)

> Demo Repo

> **[GitHub - UchiTesting/mysql-test](https://github.com/UchiTesting/mysql-test)**
>
> Contribute to UchiTesting/mysql-test development by creating an account on GitHub.
