Ozarraga_AB
Ozarraga_AB

Reputation: 979

nullreferenceexception was unhandled on filling up array - c#

I am trying to retrieve all names that was saved at the sql database
then suddenly I got the

nullreferenceexception was unhandled on filling up array - Object reference not set to an instance of an object.

this was my code:

I got the error at the

  imgName[i] = Convert.ToString(dt.Rows[i]["FinalImageName"]);  

in that part, I'd like to fill imgName array all the name

how could i fix it?, help please

string c_string = "server=.\\sqlexpress;database=Blue;trusted_connection=true";

public static string ImageToShow;
private int NumOfFiles;
private string[] imgName;

SqlConnection c = new SqlConnection(c_string);
//SqlCommand cm = new SqlCommand(cmd,c);

private void frmMain_Load(object sender, EventArgs e) {
    SqlConnection c = new SqlConnection(c_string);

    try {
        c.Open();
    }
    catch (SqlException ee) {
        MessageBox.Show(ee.Message);
    }
    finally {
        c.Close();
    }
    updateData();
}

private void updateData() {

    SqlConnection c = new SqlConnection(c_string);

    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM FinalImages", c);

    DataTable dt = new DataTable();
    da.Fill(dt);

    NumOfFiles = dt.Rows.Count;
    for (int i = 0; i < dt.Rows.Count; i++) {
        imgName[i] = Convert.ToString(dt.Rows[i]["FinalImageName"]);

    }
}​

Upvotes: 0

Views: 572

Answers (3)

Adrian Iftode
Adrian Iftode

Reputation: 15663

You need to instantiate the imgName array .

private void updateData()
{
    {

        SqlConnection c = new SqlConnection(c_string);

        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM FinalImages", c);

        DataTable dt = new DataTable();
        da.Fill(dt);

        NumOfFiles = dt.Rows.Count;
        imgName = new string[NumOfFiles];
        for (int i = 0; i < NumOfFiles; i++)
        {
            imgName[i] = Convert.ToString(dt.Rows[i]["FinalImageName"]);

        } 
    }
}

Upvotes: 2

Guffa
Guffa

Reputation: 700342

You haven't created an array, you have only created the variable that can hold the reference to the array.

Create the array once you know how many rows there are:

imgName = new string[dt.Rows.Count];

Upvotes: 1

Dr. Rajesh Rolen
Dr. Rajesh Rolen

Reputation: 14285

That is because you have not specified the size of array... either specify size of array or use arraylist / generic list

Try this: include namespace : using System.Collections.Generic;

string c_string = "server=.\\sqlexpress;database=Blue;trusted_connection=true";

    public static string ImageToShow; 
    private int NumOfFiles;
    //private string[] imgName;
   List<string> imgName= new List<string>();

SqlConnection c = new SqlConnection(c_string);
        //SqlCommand cm = new SqlCommand(cmd,c);


   private void frmMain_Load(object sender, EventArgs e)
    {
        SqlConnection c = new SqlConnection(c_string);

        try
        {
            c.Open();
        }
        catch (SqlException ee)
        {
            MessageBox.Show(ee.Message);
        }
        finally
        {
            c.Close();
        }
        updateData();
    }

private void updateData()
    {

        SqlConnection c = new SqlConnection(c_string);

        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM FinalImages", c);

        DataTable dt = new DataTable();
        da.Fill(dt);

        NumOfFiles = dt.Rows.Count;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
           // imgName[i] = Convert.ToString(dt.Rows[i]["FinalImageName"]);
           imgName.Add(Convert.ToString(dt.Rows[i]["FinalImageName"]));
        } 
    }

Upvotes: 1

Related Questions