Problem with string array?

Ken Ekholm 151 Reputation points
2023-07-04T16:34:34.92+00:00

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?

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();
        }
    }
}
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,354 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,201 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 48,686 Reputation points Microsoft Vendor
    2023-07-11T07:28:03.78+00:00

    Hi @Ken Ekholm , Welcome to Microsoft Q&A.

    Your error is because you only declared the array but didn't initialize it.

    You can use List<string> instead of Array.

    If you need an array, just use string[] values = valuesList.ToArray(); to get the array at the end, and the declaration of this values can also be placed before the database using for global use.

    Use parameterized queries to avoid sql injection.

    using (var connection = new MySqlConnection(connString))
    {
        connection.Open();
        string query = "SELECT * FROM showvalues WHERE idvalue = @idStatus";
        using (var command2 = new MySqlCommand(query, connection))
        {
            command2.Parameters.AddWithValue("@idStatus", idStatus);
            
            using (var reader2 = command2.ExecuteReader())
            {
                List<string> valuesList = new List<string>();
    
                while (reader2.Read())
                {
                    listBoxMeasurement.Items.Add(reader2.GetString("textinfo"));
                    valuesList.Add(reader2.GetString("textinfo"));
                }
    
                string[] values = valuesList.ToArray();
                connection.Close();
            }
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 69,276 Reputation points
    2023-07-04T17:04:20.36+00:00

    Arrays in c# are not dynamic. You declared an empty array, but try to set a value. Not sure why it is not subscript out of range.

    also your sql allows sql injection, and should be fixed.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.