1

This ultra simple code doesn't work... It gives me the

System.NullReferenceException: 'Object reference not set to an instance of an object.'

I tried to recreate the form and the entire program, but it has not changed anything, and it's pretty much the only thing that's written in the class

private void combo_LastName_TextChanged(object sender, EventArgs e)
{
    WhereQ[0] = "LASTNAME = '" + combo_LastName.Text + "'";

    combo_FirstName.DisplayMember = "FIRSTNAME";
    combo_FirstName.DataSource = InTable_Employee.Select(WhereQ[0]);
}

Rest of the code:

private string[] WhereQ;

private readonly string InDBConn_Locl = Settings.Default.DBConn_LOCL;
private DataSet InDB = new();
private DataTable InTable_Employee = new();

public SelectUser()
{
    InitializeComponent();
}

private void SelectUser_Load(object sender, EventArgs e)
{
    CreateTable(InDBConn_Locl, "SELECT DISTINCT LASTNAME, FIRSTNAME, GID FROM EMPLOYEE", "EMPLOYEE");

    InTable_Employee = InDB.Tables["EMPLOYEE"];

    combo_LastName.DisplayMember = "LASTNAME";
    combo_LastName.DataSource = InTable_Employee;
    combo_GID.DisplayMember = "GID";
    combo_GID.DataSource = InTable_Employee;
}

private void CreateTable(string TableName, string Conn, string Query)
{
    DataTable InTable = API.SQL_Read(TableName, Conn, Query);
    InTable.Rows.InsertAt(InTable.NewRow(), 0);

    InDB.Tables.Add(InTable);
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
dlico23
  • 11
  • 1
  • Could you at least tell us which line of code throws the NullReferenceException? – phuzi Jun 21 '21 at 07:59
  • 1
    You don't initialize `WhereQ`, therefore it is `null`. – ProgrammingLlama Jun 21 '21 at 07:59
  • 1
    I've removed the `[visual-studio-2019]` tag from your question as the question is not about using the Visual Studio application. I've also removed the tags form your question title as placing tags there goes against the tagging guidelines for this site. – ProgrammingLlama Jun 21 '21 at 08:00
  • @Llama Yes, you are right, the problem was in the WhereQ, I got crazy because the error was shown in the line `combo_FirstName.DisplayMember = "FIRSTNAME";` not the one with the WhereQ. – dlico23 Jun 22 '21 at 08:15

0 Answers0